如何从图像中读取迷宫并将其转换为Python中的二进制值 [英] How to read a maze from an image and convert it to binary values in Python

查看:86
本文介绍了如何从图像中读取迷宫并将其转换为Python中的二进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要某种方式(在Python 3中)读取迷宫图像的像素,并且预期的输出应该是二进制列表[[1,1,0],[1,1,1],[1,0, 0]](例如1代表白色,0代表黑色)(应该看起来像这样,这些值只是为了示例)我不想要一种解决迷宫的方法,我所需要的只是使能给定图像的迷宫的内部表示.我希望能够做出选择的任何算法,以便能够绘制其执行过程,显示路径以及显示进度. (我不想要一种算法,我只想一种读取迷宫图像并写入新的迷宫图像以显示所选路径/显示算法进展的方法,而不管算法是什么(Dijkstra,A *,广度优先搜索...)

I want some way (In Python 3) to read pixels of a maze image and the expected output should be a binary list [[1, 1, 0], [1, 1, 1], [1, 0, 0]] (1 for white and 0 for black for example) (should look something like this, the values are for the sake of the example) I don't want a method to solve the maze, all I need is something that enable an internal representation of the maze given an image. I want to be able to make whatever the algorithm of choice to be able to draw its execution and show path and maybe show progression as well. (I don't want an algorithm, I just want a way of reading a maze image and writing new maze images to show the chosen path/ show progression of the algorithm regardless of what the algorithm is (Dijkstra, A*, Breadth-first search ...)

我尝试使用PIL和cv2,但由于我不是该领域的专家,所以我不知道该怎么做

I tried using PIL and cv2 but as I'm not an expert on the subject, I don't know what I should do

以下是一些例子:

小迷宫:

大迷宫:

推荐答案

您可以像这样加载PNG,GIF,TIF或JPEG文件,然后确保它仅具有01值并使用PIL/枕头和脾气暴躁.

You can load a PNG, GIF, TIF or JPEG file like this, then ensure it only has 0 and 1 values and process the pixels with PIL/Pillow and Numpy.

我使用了这个迷你迷宫的编辑版本:

I used this edited version of your mini-maze:

#!/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中的二进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆