pyautogui.locateOnScreen Can't locate image

33 Views Asked by At

I try to locate an image on my screen.

I try to reinstall pyautogui and Pillow But it always shows this:

Traceback (most recent call last):
  File "c:\Users\win0908\Desktop\PY\stickman.py", line 9, in <module>
    if pyautogui.locateOnScreen('stickman.png', region=(150,175,350,600), grayscale=True, confidence=0.8) != None:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\win0908\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyautogui\__init__.py", line 172, in wrapper
    return wrappedFunction(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\win0908\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyautogui\__init__.py", line 210, in locateOnScreen
    return pyscreeze.locateOnScreen(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\win0908\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyscreeze\__init__.py", line 405, in locateOnScreen
    retVal = locate(image, screenshotIm, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\win0908\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyscreeze\__init__.py", line 383, in locate
    points = tuple(locateAll(needleImage, haystackImage, **kwargs))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\win0908\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyscreeze\__init__.py", line 241, in _locateAll_opencv
    raise ValueError('needle dimension(s) exceed the haystack image or region dimensions')
ValueError: needle dimension(s) exceed the haystack image or region dimensions

This is my Code:

import pyautogui

if pyautogui.locateOnScreen('stickman.png', confidence=0.8) != None:
    print("I can see it")

Please help!

1

There are 1 best solutions below

0
Mariano On

The error is trying to tell you that the stickman.png image is larger than the region it is searching into. See the following part of the error:

 File "c:\Users\win0908\Desktop\PY\stickman.py", line 9, in <module>
    if pyautogui.locateOnScreen('stickman.png', region=(150,175,350,600), grayscale=True, confidence=0.8) != None:

It is searching in the region (150,175,350,600) (those are x, y, width, height values representing an area) which is of smaller dimensions than the stickman.png image