pyautogui 无法找到图像异常,即使它存在

问题描述 投票:0回答:1

我长期使用 Pyautogui 库进行图像跟踪。 我不知道为什么,但我刚买了一台新笔记本电脑,但它根本不适合我。 即使图像存在,它也会抛出“找不到图像”错误。 我可以保证图像路径也是绝对正确的。 即使我尝试过 就像我下面给出的简单程序,但它仍然显示错误。

有人可以帮忙吗? 真的很欣赏它。 谢谢!

from pyautogui import *

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

print(a)

if not (a == None):

    print("Got Image")

if(a==None):

    print("Oops!")

当我尝试此操作时,我收到此错误

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
python error-handling pyautogui
1个回答
0
投票

尝试使用置信度参数。像这样。 pyautogui.locateOnScreen("E:/Mark 7/Test.png",confidence=0.7)

ImageNotFoundException 实际上并不意味着没有“E:/Mark 7/Test.png”文件。

我认为,ImageNotFoundException 意味着在屏幕上找不到与图像匹配的图案。

可以使用try: except:语句正确处理异常。

© www.soinside.com 2019 - 2024. All rights reserved.