TAGS :Viewed: 9 - Published at: a few seconds ago

[ Image reading for RGB colors? ]

How can I take an image, and read each pixel in the image, and determine if it is green or not. If the former, return true, if the latter, return false. By green I don't mean 100% green, I meam like greener then red. (if you know what I mean). After that, it should output the x, y of the green spot. Now, since its not only going to be one pixel, it would be fine if it lowered the resoloution before hand to maximize speed. I have heard of the Image library, but I haven't seen a way to read each pixel and return its RGB value.

Also, it would be appreciated if you can also guide me in the direction of taking video (well, one image every so seconds.) and then manipulating those images, and returning an array of tuples. Thanks!

Answer 1


The PIL (Python Image Library) will give you some of the functionality you are asking about. This example shows you how to open a .gif file and examine the RGB values at a given location.

import Image

im = Image.open('image.gif')
rgb_im = im.convert('RGB')
r, g, b = rgb_im.getpixel((1, 1))

print r, g, b
(65, 100, 137)

Here are the supported Image formats and here is a basic tutorial.

The PIL is pretty popular, you should be able to find additional examples and documentation via google. Also there is no official support for Python 3.x (yet?), but you can get an unofficial version of the PIL here - looks like Windows only though.

Another tutorial.