代码改变世界

使用python 3.x 对pythonchallenge-----16的解答过程

2017-09-22 15:21  太烦人  阅读(255)  评论(0编辑  收藏  举报
pythonchallenge-16地址 : http://www.pythonchallenge.com/pc/return/mozart.html


题目解析:网站标题是 let me get this straight  
图片中每行有一个红色的区域,把红色的区域右边的放到前面,红色区域左边的放到后面,然后对齐

解题过程:
 
#http://www.pythonchallenge.com/pc/return/mozart.html
from PIL import Image
path = u'./other/mozart.gif'
img = Image.open(path)
imgnew = Image.new("P",(640,480),255)

print(img.getbbox())
print(img)

for i in range(480):
    z = 0
    for j in range(2,638):
        if  img.getpixel((j,i)) == img.getpixel((j -2 ,i) )== img.getpixel((j + 2,i)) == 195:
            z = j

    box_before  = (0,i,z,i+1)
    box_after  = (z,i,640,i+1)
    region_before = img.crop(box_before)
    region_after = img.crop(box_after)
    imgnew.paste(region_after, (0,i))
    imgnew.paste(region_before, (640-z,i))

imgnew.show()

 答案:romance