Reputation: 69
I have taken a JPG image using a camera and wish to detect a yellow matchbox in the picture. I was trying to use the Python Imaging Library. I tried to find the RGB combination for yellow, and it turns out that R and G are always 255 for any shade of yellow. In the image however, there are no values of 255. What am I doing wrong? How can I solve this problem? This is my attempted code:
for x in range(1,2592):
for y in range(1,1456):
p = im.getpixel((x,y))
if p[0]>150 and p[1]>150:
print p
Unfortunately, no coordinates turn up.
Upvotes: 0
Views: 2529
Reputation: 23231
Two issues:
Depending on the image format, the color may be in a 0.0
to 1.0
space.
(150, 150, 255)
, while matching your test would not be considered yellow. In fact it's blue. You should test that red and green are large enough, but also that blue is small enough.
Putting those together:
for x in range(1,2592):
for y in range(1,1456):
p=im.getpixel((x,y))
if p[0] > 150 and p[1] > 150 and p[2] < 150: # added p[2] < 150 for blue
print p, "is yellow"
else:
print p, "is not yellow" # see what range you have
Upvotes: 1