Python Challenge [9]

Since I’m less a fan of images than of working with content directly from a site, I added functionality to this challenge to grab the useful text directly.

I don’t seem to be much a fan of the Image library. As such, it took me longer than it should have to wrap my head around this one. It turns out that the a solution, using the ImageDraw function, makes me feel like an idiot. Draw.polygon is a pretty simple and fundamental way to build graphics and, after the fact, I can see several potential uses ranging from mapping to dynamic drawings.

Just for good measure, we might as well pull the list from the site using urllib, some regex and eval to make it a tuple.


from PIL import Image, ImageDraw
import urllib, re, os

url = "http://huge:file@www.pythonchallenge.com/pc/return/good.html"


rawhtml = urllib.urlopen(url).read().replace('\n','')
raw = re.search('(([0-9]+),)+[0-9]+',rawhtml)
first_list = eval(raw.group(0))

#or in one line
first = eval(re.search('(([0-9]+),)+[0-9]+',urllib.urlopen(url).read().replace('\n','')).group(0))

second = eval(re.search('(?<=second:)(([0-9]+),)+[0-9]+',urllib.urlopen(url).read().replace('\n','')).group(0))

Ok, now that we have our tuples, on to the actual challenge.


img = Image.new('RGB', (500,500))
draw = ImageDraw.Draw(img)

draw.polygon(first,fill='white')
draw.polygon(second,fill='white')
img.save('09.jpg')

Check the image and mind the horns; I got a great laugh out of an improper solution to the puzzle, once I had the resulting image. Nice Easter egg.