P. Pat
P. Pat

Reputation: 21

Python pyautogui - Problem with location print when not detecting image

So I made a program that detects images and clicks on them when they appear and it was working fine but one day (idk if i deleted something that I shouldn't) but it now raises exception when there isn't that image on screen. [The code and the exception][1]

THE CODE IS SIMPLY:

import pyautogui
location1 = pyautogui.locateCenterOnScreen('velaki.png')
print(location1)

AND THEN THE ERROR IS:

Traceback (most recent call last):
File "C:\Users\Patroklos\Desktop\FoE\taxini.PY", line 3, in <module>
    location1 = pyautogui.locateCenterOnScreen('velaki.png')
  File "C:\Python34\lib\site-packages\pyscreeze\__init__.py", line 300, in locateCenterOnScreen
    coords = locateOnScreen(image, **kwargs)
  File "C:\Python34\lib\site-packages\pyscreeze\__init__.py", line 271, in locateOnScreen
    retVal = locate(image, screenshotIm, **kwargs)
  File "C:\Python34\lib\site-packages\pyscreeze\__init__.py", line 255, in locate
    points = tuple(locateAll(needleImage, haystackImage, **kwargs))
  File "C:\Python34\lib\site-packages\pyscreeze\__init__.py", line 249, in _locateAll_python
    raise ImageNotFoundException('Could not locate the image.')
pyscreeze.ImageNotFoundException: Could not locate the image.

Normally it should return None if no image is found not exception.

![1]: https://i.sstatic.net/0ozRR.png

Upvotes: 0

Views: 6867

Answers (5)

You can disable the exception by using:

import pyscreeze

pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION = False

Upvotes: 0

Attila Kiss
Attila Kiss

Reputation: 1

"NOTE: As of version 0.9.41, if the locate functions can’t find the provided image, they’ll raise ImageNotFoundException instead of returning None."

It is written: onhttps://pyautogui.readthedocs.io/en/latest/screenshot.html But I don't know ho to upgrade that modul. I have a newest.

Upvotes: -1

Hrishikesh Kulkarni
Hrishikesh Kulkarni

Reputation: 21

I had the exact same problem as you. It turns out that one of the dependencies of PyAutoGUI; PyScreeze was updated on January 6th, 2019 from version 0.1.18 to 0.1.19. And, version 0.1.19 raises an ImageNotFound exception sometime after not being able to find the image which terminates your script instead of returning a 'None'.

All you need to do is downgrade PyScreeze from 0.1.19 to 0.1.18 and it works just fine again.

Upvotes: 2

P. Pat
P. Pat

Reputation: 21

Seems like there was a new version (0.9.41) that raises exception instead of None and I didn't notice.

Upvotes: 1

arielbi.289
arielbi.289

Reputation: 21

If the image you are trying to locate was not save with the same resolution that is showing, the function pyautogui.locateCenterOnScreen('velaki.png') will never find it. Also you can use the library opencv and pass a confidence value and the image will not have to match 100%.

Upvotes: 1

Related Questions