asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.47k stars 1.27k forks source link

`locateAllOnScreen` raises `pyscreeze.ImageNotFoundException` #864

Open eight04 opened 5 months ago

eight04 commented 5 months ago
>>> list(pyautogui.locateAllOnScreen("test.png"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\eight04\dev\valkyrie-connect-auto\.venv\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.178)

While locateOnScreen raises pyautogui.ImageNotFoundException correctly:

>>> pyautogui.locateOnScreen("test.png")
Traceback (most recent call last):
  File "C:\Users\eight04\dev\valkyrie-connect-auto\.venv\Lib\site-packages\pyautogui\__init__.py", line 172, in wrapper
    return wrappedFunction(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\eight04\dev\valkyrie-connect-auto\.venv\Lib\site-packages\pyautogui\__init__.py", line 210, in locateOnScreen
    return pyscreeze.locateOnScreen(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\eight04\dev\valkyrie-connect-auto\.venv\Lib\site-packages\pyscreeze\__init__.py", line 405, in locateOnScreen
    retVal = locate(image, screenshotIm, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\eight04\dev\valkyrie-connect-auto\.venv\Lib\site-packages\pyscreeze\__init__.py", line 383, in locate
    points = tuple(locateAll(needleImage, haystackImage, **kwargs))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\eight04\dev\valkyrie-connect-auto\.venv\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.167)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\eight04\dev\valkyrie-connect-auto\.venv\Lib\site-packages\pyautogui\__init__.py", line 174, in wrapper
    raise ImageNotFoundException  # Raise PyAutoGUI's ImageNotFoundException.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pyautogui.ImageNotFoundException

I guess the problem is that locateAll returns a generator, hence you have to wrap its __next__ method to catch the error, or use yield from.

matthewjerome commented 4 months ago

It looks like this is an issue related to pyscreeze - please try to install pyscreeze at the version 0.1.29 as described in this issue