I'm trying to make a program which takes in an image and looks throughout the image to find a colour, lets say blue, and give out the coordinates of that point in the image which has that colour.
-
2Please visit and check [how to ask a good question](https://stackoverflow.com/help/how-to-ask) [This](https://stackoverflow.com/questions/12187354/get-rgb-value-opencv-python) seems your solution. Please make a good search before asking a question. – Yunus Temurlenk Feb 04 '20 at 07:05
3 Answers
You can do that very simply with Numpy which is what underpins most image processing libraries in Python, such as OpenCV, or skimage, or Wand. Here I'll do it with OpenCV, but you could equally use any of the above or PIL/Pillow.
Using this image which has a blue line on the right side:
#!/usr/bin/env python3
import cv2
import numpy as np
# Load image
im = cv2.imread('image.png')
# Define the blue colour we want to find - remember OpenCV uses BGR ordering
blue = [255,0,0]
# Get X and Y coordinates of all blue pixels
Y, X = np.where(np.all(im==blue,axis=2))
print(X,Y)
Or, if you want them zipped into a single array:
zipped = np.column_stack((X,Y))
If you prefer to use PIL/Pillow, it would go like this:
from PIL import Image
import numpy as np
# Load image, ensure not palettised, and make into Numpy array
pim = Image.open('image.png').convert('RGB')
im = np.array(pim)
# Define the blue colour we want to find - PIL uses RGB ordering
blue = [0,0,255]
# Get X and Y coordinates of all blue pixels
Y, X = np.where(np.all(im==blue,axis=2))
print(X,Y)

- 191,897
- 31
- 273
- 432
-
Using numpy with PIL, it appears this returns Y,X not X,Y: `X,Y = np.where(np.all(im==blue,axis=2))` – Lukus Mar 24 '22 at 17:32
-
@Lukus Yes, thank you. I have edited the correction into my answer. – Mark Setchell Mar 24 '22 at 18:13
Just noting a problem on the answer by Mark Setchell.
The X and Y coords are returned the other way around so X,Y = np.where(np.all(im==blue,axis=2))
changes to Y,X = np.where(np.all(im==blue,axis=2))
, this may just be a glitch I have though.

- 21
- 2
-
Mark also answerwed using PIL. You answer seems to be redundant. – Siva Shanmugam Oct 27 '20 at 05:04
-
@SivaShanmugam I added the try/except and changed the order of x,y variable allocation – Hackernoob666 Oct 27 '20 at 05:22
-
Yes, I saw that. It would be nice if you also exlpain why code acts likes that when you changed the order of X and Y. – Siva Shanmugam Oct 27 '20 at 05:26
-
why the code acts like what? when I tested the code it returned y before x, so it was off the screen often. that might have been a glitch though. I just did some more testing and realised that the error was turned up because of the x,y being the wrong way around, so this answer is not very useful s have changed it. – Hackernoob666 Oct 28 '20 at 22:43
In order to do so, you need a few pieces of information, including the height and width of the image in pixels, as well as the colormap of the image. I have done something similar to this before, and I used PIL (Pillow) to extract the color values of each individual pixel. Using this method, you should be able to reformat the pixel colour values into a two-dimensional array (array[x][y], where x is the x-coordinate and y is the y-coordinate, for easy comparison) and compare the individual pixel values with a specified RGB value.
If you have an image of unknown height and width, you could do the following to obtain the image height and width:
from PIL import Image
image = Image.open('path/to/file.jpg')
width, height = image.size
After this, you can use the following to get the pixel color values in RGB format in a list:
pixval = list(image.getdata())
temp = []
hexcolpix = []
for row in range(0, height, 1):
for col in range(0, width, 1):
index = row*width + col
temp.append(pixval[index])
hexcolpix.append(temp)
temp = []
You can then do a comparison to find pixels that match your specified colour

- 202
- 1
- 9