Get pixel color from window in background

I would like to get the color of a particular pixel in Python, from a specific window, witch is in the background, using its X Y coordinates, Iam using Windows10. (Im trying to write a script, witch bring window to front, when the specific pixels changed)

I try to modify this code below, but I cant get info from other window, only from the screen.

def get_pixel_colour(i_x, i_y):
    import win32gui
    i_desktop_window_id = win32gui.GetDesktopWindow()
    i_desktop_window_dc = win32gui.GetWindowDC(i_desktop_window_id)
    long_colour = win32gui.GetPixel(i_desktop_window_dc, i_x, i_y)
    i_colour = int(long_colour)
    return (i_colour & 0xff), ((i_colour >> 8) & 0xff), ((i_colour >> 16) & 0xff)

print get_pixel_colour(0, 0)

Upvotes: 2

Views: 2171

Answers (1)

Mariobarbosa777
Mariobarbosa777

Reputation: 56

I had the same problem and i create a library to work with minimized windows, here is the full code

https://github.com/mariobarbosa777/minwinpy/blob/master/minwinpy.py

You must run as admin vs-code and "your windows name" too

For solving your get color pixel problem dowload minwinpy.py and use this code:

from minwinpy import minwinpy

#x, y pixel to get RGB color 
pos=[497,72]

WindowsName="Your windows name"
yourWindows= minwinpy(WindowsName)

print (yourWindows.GetPixelRGBColor(pos))

Upvotes: 3

Related Questions