Ross Wan's World!

Python, Ajax, PHP and Linux.

Posts Tagged ‘getpixel’

The Python Challenge Lv.7

Posted by Ross Wan 于 2011/08/31

Lv.7


打开网页,显示的是一张河岸的图片, 特别的是, 图片中央有一细条”灰色带”.将图片另存为”oxygen.png”, 用图片编辑器打开, 放大到最大的倍数.可以看到,那色带是一长方形的灰度色带(left: 0px, top: 43px, right: 607px, bottom: 51px), 它又是由大小为7px * 9px的小长方形灰度块(除了第1块是 5px * 9px)组成的. 灰度的值是0~255, 正好跟 Ascii 相对应, 思路出来了, 下面交给 Python 来处理:

from PIL import Image

if __name__ == '__main__':
    img = Image.open('oxygen.png')
    for p in range(4, 607, 7):
        print(chr(int(sum(img.getpixel((p,43))[:3])/3)), end='')
    print()


注意, 处理图片需要用到 PIL 库, Python 3的 Windows 编译版可以到以下网址下载: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil


运行结果:

smart guy, you made it. the next level is [105, 110, 116, 101, 103, 114, 105, 116, 121]


可知要对 “[105, 110, 116, 101, 103, 114, 105, 116, 121]”进行映射就得到答案了;现将上面的代码改写,一次性的得到答案:)

from PIL import Image
import re

if __name__ == '__main__':
    img = Image.open('oxygen.png')
    map_string = ''.join(map(lambda p: chr(int(sum(img.getpixel((p, 43))[:3])/3)), range(4, 607, 7)))
    #print(map_string)
    print('\nKey Words: ', end='')
    for i in re.finditer(r'\d+', map_string):
        print(chr(int(i.group())), end='')
    else:
        print()


显示结果:

Key Words: integrity

可知下一关的网址是: http://www.pythonchallenge.com/pc/def/integrity.html

Have fun :)

Posted in Python | Tagged: , , , | Leave a Comment »