Skip to content

Instantly share code, notes, and snippets.

@taddeimania
Created May 19, 2015 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taddeimania/301117b5e21639ef38e7 to your computer and use it in GitHub Desktop.
Save taddeimania/301117b5e21639ef38e7 to your computer and use it in GitHub Desktop.
Show this file as an image
from PIL import Image
from math import sqrt
def get_color_value(contents, loc):
try:
return ord(contents[loc])
except IndexError:
return 0
def get_color_triplet(contents, loc):
red = get_color_value(contents, loc)
green = get_color_value(contents, loc + 1)
blue = get_color_value(contents, loc + 2)
return red, green, blue
def main():
contents = open(__file__).read()
size_of = len(contents)
square_of = sqrt(size_of)
color_values = []
loc = 0
for bit in contents:
color_group = get_color_triplet(contents, loc)
color_values.append(color_group)
loc += 3
rows = int(square_of/2)
img = Image.new('RGB', (int((size_of/3) / rows), rows), "black")
pixels = img.load()
pixel_loc = 0
for i in range(img.size[0]):
for j in range(img.size[1]):
pixels[i, j] = color_values[pixel_loc]
pixel_loc += 1
img.show()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment