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

Why is pyautogui saying "ImageNotFoundException" instead of "Nope nothing there"?

Trying to make image recognition program. Code:

from pyautogui import *
import pyautogui
import time
import keyboard
import random
import win32api, win32con

while 1:
    if pyautogui.locateOnScreen('Dassault Falcon 7X.png', confidence=0.8,minSearchTime=5) != None:
        print ("I can see it!")
        time.sleep(0.5)
    else:
        print ("Nope nothing there")

For some reason it keeps giving me imagenotfoundexception in red instead of what I want it to say when it doesn’t find anything, "Nope nothing there."

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

>Solution :

From the pyautogui docs:

NOTE: As of version 0.9.41, if the locate functions can’t find the provided image, they’ll raise ImageNotFoundException instead of returning None.

Your program assumes pre-0.9.41 behavior. To update it for the most recent version, replace your if-else blocks with try-except:

from pyautogui import locateOnScreen, ImageNotFoundException

while True:
    try:
        pyautogui.locateOnScreen('Dassault Falcon 7X.png')
        print("I can see it!")
        time.sleep(0.5)
    except ImageNotFoundException:
        print("Nope nothing there")
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