Python Challenge [11]

Continuing with all things PIL, we’re going to get a little evil.

Since we’ve realized that we can use urllib to avoid the step of separately downloading content from a site, we’ll stick with that.

Start with making our import declarations

from PIL import Image
import urllib,sys

Then we’ll use a neat function size() to grab the dimensions of the image. This might be useful for skimming websites for images in the future and finding some characteristics of the image independent of css restrictions imposed on it.

def get_image_dimensions():
	url = ""
	file = urllib.urlopen(url).read()
	jpg =
	print jpg.size

As for the real solution…

def get_img():
	url = ""
	img = urllib.urlretrieve(url,'cave.jpg')
img ='cave.jpg')
w,h = img.size[0], img.size[1]
print "Image info:",img.format, img.size, img.mode
new =,(w//2, h))
for i in range(w*h):
    y, x = divmod(i, w)
    p = img.getpixel((x,y))
    if i%2: #even==info, odd==photo