Slide 24
Slide 24 text
from PIL import Image
# Open the image in read mode
im = Image.open('.png', 'r')
# pixels is an object which allows access to
# individual pixels
pixels = im.load()
# Get the size of the picture
width, height = im.size
binary_ans = ''
for y in xrange(height): # Iterate through each pixel
for x in xrange(width):
#pixels[x, y] returns a tuple with RGB vals
red_pix = pixels[x, y][0]
green_pix = pixels[x, y][1]
blue_pix = pixels[x, y][2]
#print pixels[x, y]
if red_pix == 255:
binary_ans += '1'
elif red_pix == 254:
binary_ans += '0'
if green_pix == 255:
binary_ans += '1'
elif green_pix == 254:
binary_ans += '0'
if blue_pix == 255:
binary_ans += '1'
elif blue_pix == 254:
binary_ans += '0'
# This just converts the binary to ASCII
answer = ''
for i in xrange(len(binary_ans)/8):
answer += chr(int(binary_ans[i*8:i*8+8], 2))
f=open('answer.txt','w')
f.write(answer)
f.close()
#print answer