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."
>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")