0%

Python Challenge (Level 16)

第16关

一张杂乱无章的图片,我们把它放大后会发现它的每一行都有一小段紫色的像素。然后网页的title是let me get this straight. 那我们就把每一行的紫色像素都对齐,然后该行的其余像素根据与紫色像素的相对位置进行平移。其实就是循环移位,把紫色像素对齐。这里我把紫色像素移到行首。

代码用到了Numpy,这是一个很有用的科学计算库,作为Pythoner,很有必要看一下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- coding:utf-8 -*-

from PIL import Image
import numpy as np

im = Image.open('mozart.gif')
imdata = list(im.getdata())
imdata = np.array(imdata)
imdata = imdata.reshape((480, 640))
imdata2 = np.zeros(imdata.shape)

for row in range(imdata.shape[0]):
# 紫色线段坐标
idx = np.where(imdata[row, :] == 195)
idx = idx[0][0] - 1
# 循环平移,将紫色段移到每一行开头
imdata2[row, :] = np.r_[imdata[row, idx : 640], imdata[row, 0 : idx]]

imdata2.shape = (480 * 640, )
im.putdata(imdata2)
im.show()

重新排列后的图片显示出romance这个单词。http://www.pythonchallenge.com/pc/return/romance.html