简体   繁体   中英

pywin32: Get color by using coordinates

I wrote this code to make a mouse click on the x,y position 100,200 and after that I'm pressing the backspace button:

import win32api, win32con
import time

def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

def press_back():
    win32api.keybd_event(0x08,0,0,0) #click backspace
    time.sleep(0.1)
    win32api.keybd_event(0x08,0,2,0) #release backspace

click(100,200)
press_back()

What I want to do now is to check if the color at 100,200 is red. How can I do this?

EDIT: I have the solution...

color = win32gui.GetPixel(win32gui.GetDC(win32gui.GetActiveWindow()), 100 , 200)

thanks anyways

With your answer

color = win32gui.GetPixel(win32gui.GetDC(win32gui.GetActiveWindow()), 100 , 200)

you get an integer, which for someone (like me) could be not really friendly.

If you want an RGB tuple, you can check this answer: RGB Int to RGB - Python .

def rgbint2rgbtuple(RGBint):
    blue =  RGBint & 255
    green = (RGBint >> 8) & 255
    red =   (RGBint >> 16) & 255
    return (red, green, blue)

Finally, you have tho check if the color is red, that could be not so easy if you accept also similar colors, not only and simply the pure red (that is (255, 0, 0)).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM