Python Challenge[27]

[Level 27]

Python Challenge[27]_第1张图片

Title: between the tables

图片链向的地址即是下一关的url,但是需要用户名和密码。从源码提示did you say gif?下载到zigzag.gif。查看了图片的各属性,没有头绪。

或许我们先要知道什么是 [palette] [p1],嗯,[调色板] [p2],或者[更详细的调色板] [p3]。
[p1]: https://en.wikipedia.org/wiki/Palette_(computing)
[p2]: https://zh.wikipedia.org/wiki/BMP#.E8.B0.83.E8.89.B2.E6.9D.BF
[p3]: http://www.360doc.com/content/10/0928/15/2790922_57060786.shtml

调色板相当于建立了颜色索引,我们要把它们还原颜色。

from PIL import Image
img = Image.open('zigzag.gif')
data = img.tobytes()
p = img.getpalette()[::3]
table = bytes.maketrans(bytes([i for i in range(256)]),bytes(p))
trans = data.translate(table)

对齐转换前后的数据,找出不同:

zipped = list(zip(data[1:],trans[:-1]))
indices = [i for  i,p in enumerate(zipped) if p[0]!=p[1]]
new = Image.new(img.mode,img.size)
color = [255,]*len(data)
for i in indices:
  color[i] = 0
new.putdata(color)
new.show()

显示:

Python Challenge[27]_第2张图片

notword,中间有一把钥匙- key。然而并不是下一关的钥匙。“不同”组成的信息呢?

import bz2
import keyword
diff = [p[0] for p in zipped if p[0]!=p[1]]
text = bz2.decompress(bytes(diff)).decode()
print(set(i for i in text.split() if not keyword.iskeyword(i)))

打印出:

{'switch', 'repeat', 'exec', 'print', '../ring/bell.html'}

repeatswitch 才是用户名和密码,[Level 28]

小结

或者这样找diff:
diff = list(filter(lambda p: p[0] != p[1], zipped))

  1. [Image.getpalette()] [s1] 返回调色板列表数据。
  2. [enumerate(iterable, start=0)] [s2] 将可迭代对象做成索引序列。
  3. [zip()] [s3] 接受可迭代对象,将对应的元素打包再组成新的可迭代对象。
  4. [keyword.iskeyword(s)] [s4] 判断s是否是 python 关键字。在python3中,execprint不再是关键字。
    [s1]: https://pillow.readthedocs.io/en/4.0.x/reference/Image.html#PIL.Image.Image.getpalette
    [s2]: https://docs.python.org/3/library/functions.html#enumerate
    [s3]: https://docs.python.org/3/library/functions.html#zip
    [s4]: https://docs.python.org/3/library/keyword.html#keyword.iskeyword

Python Challenge Wiki

你可能感兴趣的:(Python Challenge[27])