Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PyAutoGUI "PixelMatchesColor" always TRUE

I’m trying to check a pixel on my screen using pyautogui "PixelMatchesColor" function, but it always runs the code even if the pixel color is not correct.

Here’s my code:

def myFunction():
    im = pyautogui.screenshot()
    color = im.getpixel((1992, 1435))
    print(color)
    try:
        pyautogui.pixelMatchesColor(1992, 1435, (85, 214, 142))
        print("Color found")
    except:
        print("Color not found")

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(16, 52, 154)
Color found

Do you know where I’m making a mistake?

>Solution :

As seen in https://pyautogui.readthedocs.io/en/latest/screenshot.html#pixel-matching, pixelMatchesColor returns a boolean value True or False to indicate whether the color matches.

To branch on a boolean value, use an if statement. A try statement is for catching exceptions, which is not relevant here.

if pyautogui.pixelMatchesColor(1992, 1435, (85, 214, 142)):
    print("Color found")
else:
    print("Color not found")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading