A error I cannot solve

I was getting an error and couldn’t find anything on it
code is from https://github.com/KianBrose/Image-Recognition-Botting-Tutorial/blob/master/stickman.py

#This script locates the image stickman.png in the region we give it and tell you if it can see it

from pyautogui import * 
import pyautogui 
import time 
import keyboard 
import random
import win32api, win32con

while 1:
    if pyautogui.locateOnScreen('hope.png', region=(150,175,350,600), grayscale=True, 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)

after running module i’m given this error

Traceback (most recent call last):
  File "R:\hhhh\i wish.py", line 11, in <module>
    if pyautogui.locateOnScreen('hope.png', region=(150,175,350,600), grayscale=True, confidence=0.8) != None:
  File "C:\Users\polan\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyautogui\__init__.py", line 172, in wrapper
    return wrappedFunction(*args, **kwargs)
  File "C:\Users\polan\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyautogui\__init__.py", line 210, in locateOnScreen
    return pyscreeze.locateOnScreen(*args, **kwargs)
  File "C:\Users\polan\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyscreeze\__init__.py", line 405, in locateOnScreen
    retVal = locate(image, screenshotIm, **kwargs)
  File "C:\Users\polan\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyscreeze\__init__.py", line 383, in locate
    points = tuple(locateAll(needleImage, haystackImage, **kwargs))
  File "C:\Users\polan\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

I am not at all experienced

Exactly as it says. PyAutoGUI is refusing to try to locate hope.png on the screen within the region (150,175,350,600), because that image could not possibly fit inside that region (it is either too tall, or too wide, or both).

The “needle” and “haystack” terminology in the error message comes from the English idiom.

Where the code says (150,175,350,600), make sure that this means what you expect it to mean, by checking the documentation. For example, perhaps you expected the 350 and 600 to mean a height and width, but they actually mean right-most X and bottom-most Y. Also check the image; maybe it’s bigger than you expected.

Another way to find information is to copy and paste the last line of the error into a search engine. When I try this, I immediately see a few results that look helpful, such as:

https://www.reddit.com/r/learnpython/comments/vsdeav/

1 Like

I still cannot find what is causing this issue
I’ve used this script to confirm the size of my screen


im1 = pyautogui.screenshot(region=(0,0,1920,1080))
im1.save(r"./savedimage.png")

then added the proper cords

if pyautogui.locateOnScreen('hope.png', region=(0,0,1920,1080), grayscale=True, confidence=0.8) != None:

Yet the error returned is the same

What happens when you try using an image editor to open the hope.png file and tell you the size of the image?

1 Like
>>> import pyautogui

>>> screenWidth, screenHeight = pyautogui.size() # Get the size of the primary monitor.
>>> screenWidth, screenHeight
(2560, 1440)

Thank you, the image was too big and I had not realized

Now i have encounter another problem
when the image is nolonger in view it gives an error and stops looping

I have found a work around, Thank you for the help