我想要某种方式(在Python 3中)读取迷宫图像的像素,预期输出应该是二进制列表 [[1, 1, 0], [1, 1, 1], [1, 0, 0]](例如 1 代表白色,0 代表黑色)(应该看起来像这样,这些值是为了示例)我不需要一种方法来解决迷宫,我需要的只是能够启用的方法给定图像的迷宫的内部表示。我希望能够选择任何算法来绘制其执行并显示路径,也许还可以显示进度。 (我不需要算法,我只是想要一种读取迷宫图像并编写新迷宫图像的方法来显示所选路径/显示算法的进展,无论算法是什么(Dijkstra、A*、宽度优先)搜索...)
我尝试使用 PIL 和 cv2,但由于我不是该主题的专家,所以我不知道我应该做什么
以下是一些迷宫示例:
小迷宫:
大迷宫:
最佳答案
您可以像这样加载 PNG、GIF、TIF 或 JPEG 文件,然后确保它只有 0
和 1
值,并使用 PIL/Pillow 和处理像素Numpy。
我使用了你的迷你迷宫的编辑版本:
#!/usr/bin/env python3
from PIL import Image
import numpy as np
# Open the maze image and make greyscale, and get its dimensions
im = Image.open('maze.png').convert('L')
w, h = im.size
# Ensure all black pixels are 0 and all white pixels are 1
binary = im.point(lambda p: p > 128 and 1)
# Resize to half its height and width so we can fit on Stack Overflow, get new dimensions
binary = binary.resize((w//2,h//2),Image.NEAREST)
w, h = binary.size
# Convert to Numpy array - because that's how images are best stored and processed in Python
nim = np.array(binary)
# Print that puppy out
for r in range(h):
for c in range(w):
print(nim[r,c],end='')
print()
结果如下:
000000000000000000001111111111111111111000000000000000000000000000000000000000000000000000000000000
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111110000000000000000000000000000000000000000000000000000000000111111111111111111100
001111111111111111110000000000000000000000000000000000000000000000000000000000111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
000000000000000000001111111111111111110011111111111111111110000000000000000000111111111111111111100
000000000000000000001111111111111111110011111111111111111100000000000000000000011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111110111111111000000000001000000000000001100111111111111111110011111111111111111100
001111111111111111110000000000000000000000000000000000000000111111111111111110011111111111111111100
001111111111111111100000000000000000000000000000000000000000111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
000000000000000000000000000000000000000011111111111111111110000000000000000000000000000000000000000
000000000000000000000000000000000000000011111111111111111110000000000000000000000000000000000000000
关于python - 如何在 Python 中从图像读取迷宫并将其转换为二进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57610416/