I am trying to automate some work using python code and the function "pyautogui" but I need to find a way to detect colours where the mouse is on the screen. Anyone has any solutions? Thanking in advance.
CodePudding user response:
import pyautogui
while True:
x, y = pyautogui.position()
px = pyautogui.pixel(x, y)
print(px)
CodePudding user response:
You can call pyautogui.pixel(x, y)
to obtain the (red, green, blue)
value of the pixel where the mouse is. On macOS unfortunately, the mouse cursor itself is included in the screenshot. You will have to call x, y = pyautogui.position()
to obtain the x, y coordinates, then call pyautogui.moveRel(50, 0)
to move the mouse cursor away, then call pyautogui.pixel(x, y)
to get the color of the pixel of where the mouse cursor was.
This will hopefully be corrected on macOS in a future version of PyAutoGUI.