Search code examples
pythonerror-handlingpyautogui

pyautogui could not locate image exception even though it exists


I am Using Pyautogui Library for image tracing for a long time. I don't know why but I just got a new laptop and it's not working for me at all. It throws the Image not found Error Even if image exists. I can guarantee that image path is also absolute Correct. Even I Tried a simple program Like I've Given Below But It Still Shows Me Error.

Is There Anyone Who Can Help ? Really Appreciate It. Thank You!

from pyautogui import *

a = locateOnScreen("E:/Mark 7/Test.png")

print(a)

if not (a == None):

    print("Got Image")

if(a==None):

    print("Oops!")

When I Tried This, I got This Error

Traceback (most recent call last):
  File "C:\Users\DELL INC\AppData\Roaming\Python\Python312\site-packages\pyautogui\__init__.py", line 172, in wrapper
    return wrappedFunction(*args, **kwargs)
  File "C:\Users\DELL INC\AppData\Roaming\Python\Python312\site-packages\pyautogui\__init__.py", line 210, in locateOnScreen
    return pyscreeze.locateOnScreen(*args, **kwargs)
  File "C:\Users\DELL INC\AppData\Roaming\Python\Python312\site-packages\pyscreeze\__init__.py", line 405, in locateOnScreen
    retVal = locate(image, screenshotIm, **kwargs)
  File "C:\Users\DELL INC\AppData\Roaming\Python\Python312\site-packages\pyscreeze\__init__.py", line 383, in locate
    points = tuple(locateAll(needleImage, haystackImage, **kwargs))
  File "C:\Users\DELL INC\AppData\Roaming\Python\Python312\site-packages\pyscreeze\__init__.py", line 371, in _locateAll_pillow
    raise ImageNotFoundException('Could not locate the image.')
pyscreeze.ImageNotFoundException: Could not locate the image.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\Mark 7\Test.py", line 3, in <module>
    a = locateOnScreen("E:\\Mark 7\\Test.png")
  File "C:\Users\DELL INC\AppData\Roaming\Python\Python312\site-packages\pyautogui\__init__.py", line 174, in wrapper
    raise ImageNotFoundException  # Raise PyAutoGUI's ImageNotFoundException.
pyautogui.ImageNotFoundException

Solution

  • Try using the confidence parameter. Like this. pyautogui.locateOnScreen("E:/Mark 7/Test.png", confidence=0.7)

    ImageNotFoundException doesn't actually mean there is no "E:/Mark 7/Test.png" file.

    I think, ImageNotFoundException means that a pattern matching the image cannot be found on the screen.

    You can properly handle exceptions using the try: except: statement.