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 = Image.open("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