For a programming project, I need to read pixels from a loaded image using pyglet.
I used "pyglet.image.load('map.png')开发者_如何学JAVA" to load the image, and I found out that you can retrieve the image data using img.get_image_data().
I use line mapImage.get_region(x,y,1,1).get_image_data().get_data("RGBA", 4)
to read the data from the pixel located at (x,y). This is where I get stuck. When I print the result of the line above, I get an empty space. Could anyone clarify how I extract the RGB values from the string returned by the get_data() function?
Thanks!
Your mapImage... returns a string with 4 characters representing rgba.
Try this:
pix = mapImage.get_region(x,y,1,1).get_image_data().get_data("RGBA", 4)
print 'r = ' + str(ord(pix[0]))
print 'g = ' + str(ord(pix[1]))
print 'b = ' + str(ord(pix[2]))
print 'a = ' + str(ord(pix[3]))
精彩评论