Ask Your Question

how to check in python if a pixel in an image is of a specific color? (other than black and white for grey image)

asked 2017-09-08 07:58:27 -0500

Santhosh1 gravatar image

updated 2017-09-08 07:59:25 -0500

Example image:

image description

Here I want to check if a pixel is of green color.

Only getting a BGR value from a pixel and changing it is mentioned here in the Basic Operations on Images in python

What will be the python command that I would have to use to check a pixel?

edit retag flag offensive close merge delete


YourImage[coordinate_X , coordinate_Y]] ; you'll get an array of R G B data if its green then its 0 255 0 You can also use cv2.inRange to check green color.

Ziri gravatar imageZiri ( 2017-09-08 08:02:05 -0500 )edit

@Ziri How do I check this in a condition like IF statement? (We are talking about python I hope, not C++) ๐Ÿ˜…

Santhosh1 gravatar imageSanthosh1 ( 2017-09-08 08:10:58 -0500 )edit

Try : color = YourImage[coordinate_X , coordinate_Y]] ; if ( color[0] == 0 && color[1] == 255 && color[2] ==0) --> Its green

Ziri gravatar imageZiri ( 2017-09-08 08:23:54 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-09-11 00:21:04 -0500

Santhosh1 gravatar image

I found an answer here link

b,g,r = cv2.split(img[x,y])



Since there's a warning about using split here link

numpy array has been mentioned as a good alternative

if img[98,68][1]==255 :

0=Blue, 1=Green and 2=Red

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-09-08 07:58:27 -0500

Seen: 7,186 times

Last updated: Sep 11 '17