Python Challenge [7]

Wherein we are introduced to the Python Image Library, PIL.

The clue gives us a nice clean image with an odd stripe with grey and black boxes running across it. To try a couple things out, I ran a quick test of the getpixel functionality in PIL. Because it is a rather expansive library (and this goes for performance concerns across the board) it’s better for import specific classes from libraries if you’re not going to use the full functionality of the library.

Read in the image as a string.


from PIL import Image
import urllib,StringIO
url = "http://www.pythonchallenge.com/pc/def/oxygen.png"
img = urllib.urlopen(url).read()
data = Image.open(StringIO.StringIO(img))

Isolate the top pixel row of the stripe


y = 0
while True:
	col = data.getpixel((0, y))
	if(col[0] == col[1] == col[2]):
		break
	y += 1

#how far across?
x = 0
while True:
        col = data.getpixel((x, y))
        if not(col[0] == col[1] == col[2]):
		break
	x += 1

print "first grey line: ", y, " px\nWidth: ",x, " px" 

Now we want to grab one of the off rows, but only the 7th pixel as the boxes change every 7th pixel. The part that threw me is that there is a character equivalent to color values, given by the chr() function. Weird.


out = []
for i in range(0,x,7):
	col = data.getpixel((i,y))
	out.append(chr(col[0]))

print "".join(out)

That gives us a hint as output.


hint = [105, 110, 116, 101, 103, 114, 105, 116, 121]
print ''.join([chr(i) for i in hint])