pythonchallenge Level 24

第24关:http://www.pythonchallenge.com/pc/hex/ambiguity.html

标题:from top to bottom

查看源码:图片是maze.png

也就是要走迷宫

入口在上面,出口在下面

黑色的是路,白色的是墙

路径上还有很多红点

先把迷宫走出来。

from PIL import Image

img = Image.open('maze.png')
width,heigth = img.size # 641 641
for w in range(width):
    if img.getpixel((w, 0))[0] == 0:
        # 第一行黑色的是入口
        print('入口位置:(%s,0)' % w) # 入口位置:(639,0)
    if img.getpixel((w, heigth-1))[0] == 0:
        # 最后一行黑色的是出口
        print('出口位置:(%s,%s)' % (w,heigth-1)) # 出口位置:(1,640)

得到入口是(639,0)出口是(1,640)

from PIL import Image, ImageDraw

img = Image.open('maze.png').getdata()
width,heigth = img.size # 641 641

def RBG_notWhite(img):
    '''找出RGB不等于白色的点'''
    nw = set()
    width, height = img.size
    for x in range(width):
        for y in range(height):
            if img.getpixel((x,y)) != (255,255,255,255):
                nw.add((x,y))
    print("找到%i个不是白色的点" % len(nw))
    return nw

nw = RBG_notWhite(img) # 不是白色的点即 可以走的路径
start = (639,0)
end = (1,640)
# 有四个方向
direction = [(1, 0), (0, 1), (-1, 0), (0, -1)]
queue = [end]
nextPosList = {}

while True:
    pos = queue.pop()
    if pos == start:
        break
    for d in direction:
        nextPos = (pos[0]+d[0], pos[1]+d[1])
        # 下一个点需要是非白色且没有走过
        if nextPos in nw and nextPos not in nextPosList:
            nextPosList[nextPos] = pos
            queue.append(nextPos)

newImg = Image.new(img.mode,img.size,"white")

color = []
while pos != end:
    p = img.getpixel(pos)
    color.append(p[0])
    newImg.putpixel(pos,p)
    pos = nextPosList[pos]
newImg.save("mazePath.png")
print(color)

找到迷宫出口,生成路径图mazePath.png

 

把路径上的颜色的R值打印出来,看到了熟悉的b'PK

print(bytes(path)) #b'\x00P\x00K\x00\x03\x00\x04\x00\x14\x00\x00...

再把路径放大看可以看到一格全黑,一格有颜色,再处理下,得到一段 b'PK\x03\x04\x14\...,

print(bytes(path[1::2])) b'PK\x03\x04\x14\

修改下脚本,写入压缩文件

newImg = Image.new(img.mode,img.size,"white")

path = []
while pos != end:
    p = img.getpixel(pos)
    path.append(p[0])
    newImg.putpixel(pos,p)
    pos = nextPosList[pos]

open('maze.zip','wb').write(bytes(path[1::2]))

newImg.save("mazePath.png")

得到一个maze.zip,解压之后有一个maze.jpg和mybroken.zip

解压mybroken.zip,里面有一张mybroken.gif,但是打不开,名字是broken可能不完整,先不管了

打开maze.jpg 显示lake

获得下一关地址:http://www.pythonchallenge.com/pc/hex/lake.html

posted @ 2021-12-07 10:02  OTAKU_nicole  阅读(93)  评论(0编辑  收藏  举报