Reputation: 7352
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:
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?
Upvotes: 0
Views: 2794
Reputation: 17932
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
Upvotes: 3
Reputation: 1063
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.
Upvotes: 0