1 #-*- coding:utf8-*-
2 import time
3 start = time.time()
4
5 import Image
6
7 # download the image from: http://www.pythonchallenge.com/pc/return/cave.jpg
8
9 image = Image.open("cave.jpg")
10 nsize = tuple([x / 2 for x in image.size])
11 odd = even = Image.new(image.mode, nsize)
12
13 for x in range(image.size[0]):
14 for y in range(image.size[1]):
15 if x % 2 == 0 and y % 2 == 0:
16 even.putpixel((x / 2, y / 2), image.getpixel((x, y)))
17 elif x % 2 == 0 and y % 2 == 1:
18 odd.putpixel((x / 2, (y - 1) / 2), image.getpixel((x, y)))
19 elif x % 2 == 1 and y % 2 == 0:
20 even.putpixel(((x - 1) / 2, y / 2), image.getpixel((x, y)))
21 else:
22 odd.putpixel(((x - 1) / 2, (y - 1) / 2), image.getpixel((x, y)))
23
24 even.save("even.jpg")
25 odd.save("odd.jpg")
26 print "耗时:"+str(time.time() - start)