This code from Vegasseat worked when I made my own bitmap image to count red pixels. I now want to count green pixels so I changed the code. Again, when I make my own bitmap it will count the green pixels but why won't it count them in an image like this?
And yes, although I saved it as a png, I try it saving it as a bmp. Is it because the pixels are faint? Is there a way to make this work for an image like this? Thanks

# use PIL to count pixels 
# image file needs to be a bitmap file to maintain
# green (0,255,0) and black (0,0,0) pixel colors correctly

from PIL import Image

# img_I.bmp --> 50x50 black bg + green I (times new roman, bold, 24)
img_I ="img_I.bmp").getdata()

# count green pixels 
# list(img_I) is a list of (r,g,b) color tuples for each pixel
# where (0,255,0) would be the green pixel
green_I = 0
for green in list(img_I):
    if green == (0,255,0):
        green_I += 1

print green_I  # 76

You could try print(set(img_I)) to see all the colors contained in your image.

The algorithm as posted only counts pixels that are green and only green. There is no provision for counting something that is 'almost' green.

The (0,255,0) represents the color...that's 0 parts red, 255 parts green, 0 parts blue.

The color (0,255,1) while visually VERY close, is not the 'pure' green and would not be counted.

Could this be your problem?

I know that to get a list of all the pixels in the image i used image.getdata. This gave me an object and i could iterate over it, so i could go:

import Image
i ='stuff.bmp')

for pixel in i.getdata():
    print pixel

Hope that helps

Ah wait, update! I found whats wrong. The colour that is green in this is not perfect green just like mk said, but its quite a way off. The green in your image is (0,128,0). That would explain why your program is not getting it.

Great! Thanks for the replies. One more question about it....since some green pixels are brighter than others, is there a way to make an 'if' statement for it, to cover the entire range? Something like:
if green!=0;
or something to the effect of:
I know those won't work like that but you get the idea :)

To do more with the color, you're probably going to want to look at the component parts of the tuple.

For example, the following would count any pixel with more green than red and more green than blue:

for pixel in list(img_I):
    red, green, blue = pixel
    if green > red and green > blue:
        green_I += 1

This counts pixels with any green:

for pixel in list(img_I):
    red, green, blue = pixel
    if green > 0:
        green_I += 1

Some of the 'problem' values for the first case:

  • 254,255,254 - Almost pure white
  • 254,255,0 - Almost pure yellow

Some of the 'problem' values for the second case:

  • 0,1,255 - Almost pure blue
  • 255,1,255 - Almost pure purple

If you wanted to get pixels that were 'mostly' green, I would probably use something like the first case, but make sure the color parts are more than just 1 apart. How far apart would be part of the definition of 'mostly'. And if you want to make sure it was more of a 'pure' green than say a yellow-green or blue-green, you might want to include something about the ratio of blue to red. (In a 'true' green, there are equal parts of red and blue.)

Wonderful! Those work great! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.