Search code examples
pythoncolorsmouse

Check color value of pixel on screen


I'm making a bot that automatically clicks if the mouse is not over a specific color. I have everything from positioning to clicking, but I do not know how I can check if the pixel the mouse is over is a specific color. I currently use mouse and keyboard, and google hasn't given me the results fitting for my situation.

It is also not an image, but live on the screen.

It should look something like this:

colorUnderMouse = getColorUnderMouse()
if colorUnderMouse == "FF00FF":
  mouse.click('left')
else:
  mouse.move(10, 0, absolute=False, duration=0)

Solution

  • You can use pyautogui:

    import pyautogui
    
    def getColorUnderMouse():
        x, y= pyautogui.position()
        pixel = pyautogui.screenshot(
            region=(
                x, y, 1, 1
            )
        )
        return pixel.getcolors()