0

I want to create an application that receives an image with only black and white tiles. White tiles would mean a space that you can move in, while black tile would be a space that you can't move in (a wall maybe)

In this image i want to define a starting point and a goal point (maybe with diferent colors, red and yellow) so I can study pathfinding problems

How can I read an image and process the information it has in it?

It would be sometinhg like that: enter image description here

Here I'm able to define walls, starting point, goal point in an image and I'd like to read it's data. How can I do that in python?

5
  • Can the imagine instead be encoded as a text file? You would save yourself a lot of headaches if you aren't reading and processing an actual image file. Commented Sep 5, 2014 at 3:28
  • You mean with 1's and 0's? Yeah.. this way I think I know how to do. But I'd like to work with images. Draw my brand new maze and make it solve on it's own, do you get it? Commented Sep 5, 2014 at 3:31
  • 1
    Why don't you use pygame or something similar to "draw" these tiles then solve them like that, you could start with all white tiles, then click the tiles you want to be solid, then solve Commented Sep 5, 2014 at 3:42
  • That can be a solution indeed. Do you know any links that could guide me through this? Pygame is a huge library.. Commented Sep 5, 2014 at 4:10
  • You could also use PiL (Python Image Library) to load your image and read the individual pixels. Commented Sep 7, 2014 at 12:51

2 Answers 2

3

You can use the SciPy library, which comes with many image processing tools:

from scipy import misc
img = misc.imread("lena.bmp")

You can easily access a pixel, e.g. at row 10 and column 15:

print img[10, 15]

Output:

[188 101  71]

(In this example I'm using the "Lena" test image from here.)


Depending on the image format you might want to convert it from an RGB color image to gray-scale:

img = img.mean(axis = 2)
print img[10, 15]

Output:

120.0

If you need to scale the uint8 pixel values to the range [0..1]:

img = (img / 255).round()
print img[10, 15], img[10, 6]

Output:

0.0 1.0
Sign up to request clarification or add additional context in comments.

2 Comments

is it too hard to recognize shapes from an image? For example: recognize squares so that I can make each one my maze's tiles
@RodrigoAlves: Yes, it is. There is a whole field of research on Pattern Recognition. You might find solutions for very specific problems (well defined image conditions, limited set of shapes etc.), but no general shape recognition.
0

You can use the Image module from the PIL library (which you will need to install) then do something like:

from PIL import Image
img = Image.open("PATH TO IMAGE", "MODE (probably 'r')")
pixel = image.getpixel((x,y))

x,y are the pixel coordinates, and you can get the color of that pixel.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.