Hi all,
I was recently having a look at the pywin32 module and found a multitude of approaches on how to find the color of a pixel given its coordinates, but was wondering if there was a way to find the coordinate of a pixel given its particular color, either as an rgb tuple or hex vlaue.
any help is greatly appreciated

Dani AI

Generated

What you want is a fast color search over a screenshot, then translate those matches back to screen coordinates. Instead of looping pixel-by-pixel (which will be slow, as noticed), grab a frame once and let NumPy do the comparison in vectorized form. Using MSS for capture is noticeably faster than many pure-Python approaches and works across platforms. (python-mss.readthedocs.io)

import numpy as np
import mss
import pyautogui

def hex_to_rgb(h):
    h = h.lstrip('#')
    return tuple(int(h[i:i+2], 16) for i in (0, 2, 4))

target_rgb = hex_to_rgb("#3fa2f5")  # your color
tol = 8  # per-channel tolerance

with mss.mss() as sct:
    mon = sct.monitors[1]  # primary monitor bbox
    frame = np.asarray(sct.grab(mon))[:, :, :3]   # BGRA -> BGR
    rgb = frame[:, :, ::-1].astype(np.int16)      # BGR -> RGB, avoid uint8 wrap
    diff = np.abs(rgb - np.array(target_rgb, dtype=np.int16))
    mask = np.all(diff <= tol, axis=2)
    ys, xs = np.where(mask)                       # indices of matching pixels

coords = list(zip(xs + mon["left"], ys + mon["top"]))
if coords:
    pyautogui.click(coords[0])                    # click the first match

Notes and tweaks, tying back to earlier replies:

  • is right that colors do not imply positions; you first need an image (a screenshot), then you map matches back to coordinates using the monitor offset, as above. The np.where call returns row/col indices efficiently. (numpy.org)
  • pointed you toward PIL/Pillow for grabbing; if you stick with OpenCV instead, remember MSS returns BGRA, OpenCV defaults to BGR, and color conversions like BGR<->RGB or to HSV use cv.cvtColor. For robust matching across gradients, threshold in HSV with cv.inRange. (docs.opencv.org)
  • To automate the click reliably, pyautogui.click(x, y) is sufficient, but be mindful of multi-monitor setups and DPI scaling; on Windows, set your process DPI awareness (manifest or SetProcessDpiAwarenessContext) so screenshot pixels and cursor coordinates line up. (autogui.readthedocs.io, learn.microsoft.com)

Practical tips: restrict the search to a small ROI if you know where the color should appear; increase tol if antialiasing or compression varies the color slightly.

Recommended Answers

All 5 Replies

What relation could there possibly exist between coordinates and RGB values?
Unless, you have another definition of coordinates than me.

What I meant is how would one generate a list of coordinates of pixels of a particular color. The end effect I'm trying to achieve is to have the program be able to find the location of a particular color on screen, and then clicking on it. The clicking etc I understand how to do, yet the only method I know of finding the location of a colour would be to take a screenshot of the pixel at (0,0), check if it's colour matches the one I have predetermined, and then iterate across and down the screen with a loop function or something, which is massively slow.
Thanks for your help:)

cheers for all the helps guys, got it working by using PIL, it now takes a screenshot and checks for pixels in a color range, saving the coordinates and then clicking on them later:) thanks again for your assisstance!

Im also working a project that requires finding the coordinates of a certain color on the screen and am stuck on the same part as you it would be helpful if you could let me know how you did this

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.