from pyautogui import *
import pyautogui
import time
import keyboard
import random
import win32api, win32con
while 1:
if pyautogui.locateOnScreen('orange shape.png', confidence=0.8) != None:
print ("I can see it")
time.sleep(0.5)
else:
print("I am unable to see it")
time.sleep(0.5)`
I watched this video here: https://www.youtube.com/watch?v=YRAIUA-Oc1Y&t=659s&ab_channel=KianBrose
I did everything the guy in the video said to, I wrote the code exactly the same as him, but whenever the code can’t see the image, it gives me an error instead of saying "I am unable to see it". But it works fine when it can see the image
And this is the error:
Traceback (most recent call last):
File "C:\Users\Stupid Idiot\AppData\Local\Programs\Python\Python37\lib\site-packages\pyautogui\__init__.py", line 172, in wrapper
return wrappedFunction(*args, **kwargs)
File "C:\Users\Stupid Idiot\AppData\Local\Programs\Python\Python37\lib\site-packages\pyautogui\__init__.py", line 210, in locateOnScreen
return pyscreeze.locateOnScreen(*args, **kwargs)
File "C:\Users\Stupid Idiot\AppData\Local\Programs\Python\Python37\lib\site-packages\pyscreeze\__init__.py", line 405, in locateOnScreen
retVal = locate(image, screenshotIm, **kwargs)
File "C:\Users\Stupid Idiot\AppData\Local\Programs\Python\Python37\lib\site-packages\pyscreeze\__init__.py", line 383, in locate
points = tuple(locateAll(needleImage, haystackImage, **kwargs))
File "C:\Users\Stupid Idiot\AppData\Local\Programs\Python\Python37\lib\site-packages\pyscreeze\__init__.py", line 257, in _locateAll_opencv
raise ImageNotFoundException('Could not locate the image (highest confidence = %.3f)' % result.max())
pyscreeze.ImageNotFoundException: Could not locate the image (highest confidence = 0.711)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Stupid Idiot\OneDrive\Desktop\test\orange.py", line 9, in <module>
if pyautogui.locateOnScreen('orange shape.png', confidence=0.8) != None:
File "C:\Users\Stupid Idiot\AppData\Local\Programs\Python\Python37\lib\site-packages\pyautogui\__init__.py", line 174, in wrapper
raise ImageNotFoundException # Raise PyAutoGUI's ImageNotFoundException.
pyautogui.ImageNotFoundException
>>>
I tried lowering the confidence to 0.7, but that made it continuously say "I can see it" even when the image wasn’t being shown on screen.
I also tried switching to python 3.7 but that didn’t work either.
Sorry if the question is dumb, I just started learning python 3 days ago :’)
>Solution :
pyautogui was changed in version 0.9.41 so that locateOnScreen raises an exception when the image is not found instead of returning None. You need to use try/except to catch it. That’s not a choice I would have made, but they didn’t ask me.
https://pyautogui.readthedocs.io/en/latest/screenshot.html
while 1:
try:
pyautogui.locateOnScreen('orange shape.png', confidence=0.8)
print ("I can see it")
except pyautogui.ImageNotFoundException:
print("I am unable to see it")
time.sleep(0.5)