所以,我正在使用
pyautogui
自动化 Python 库,并尝试在屏幕上找到图片的轴 (x, y)。
根据官方文档,我们有以下命令,据说可以发现屏幕上图片的轴。
>>> pyautogui.locateCenterOnScreen('looksLikeThis.png') # returns center x and y
(898, 423)
但是,它在我的屏幕上找不到图像
import pyautogui
pyautogui.FAILSAFE = False
pyautogui.moveTo(0, 0, duration=0)
# pyautogui.click(x=650, y=850, clicks=1, interval=0, button='left', duration=1) # Chrome
x, y = pyautogui.locateCenterOnScreen("resources/chrome.png")
pyautogui.click(x=x, y=y, clicks=1, interval=0, button='left', duration=1)
‣https://i.imgur.com/pSbaUlL.png
‣https://i.imgur.com/6W6Aic0.png
>>> pyautogui.locateCenterOnScreen("resources/chrome.png")
Traceback (most recent call last):
File "/Users/victorcosta/.asdf/installs/python/3.11.3/lib/python3.11/site-packages/pyautogui/__init__.py", line 172, in wrapper
return wrappedFunction(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/victorcosta/.asdf/installs/python/3.11.3/lib/python3.11/site-packages/pyautogui/__init__.py", line 204, in locateCenterOnScreen
return pyscreeze.locateCenterOnScreen(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/victorcosta/.asdf/installs/python/3.11.3/lib/python3.11/site-packages/pyscreeze/__init__.py", line 447, in locateCenterOnScreen
coords = locateOnScreen(image, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/victorcosta/.asdf/installs/python/3.11.3/lib/python3.11/site-packages/pyscreeze/__init__.py", line 405, in locateOnScreen
retVal = locate(image, screenshotIm, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/victorcosta/.asdf/installs/python/3.11.3/lib/python3.11/site-packages/pyscreeze/__init__.py", line 383, in locate
points = tuple(locateAll(needleImage, haystackImage, **kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/victorcosta/.asdf/installs/python/3.11.3/lib/python3.11/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 "<stdin>", line 1, in <module>
File "/Users/victorcosta/.asdf/installs/python/3.11.3/lib/python3.11/site-packages/pyautogui/__init__.py", line 174, in wrapper
raise ImageNotFoundException # Raise PyAutoGUI's ImageNotFoundException.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pyautogui.ImageNotFoundException
pyautogui
中pyscreeze
的源代码中,它检测到cv2
,如下所示:
_useOpenCV: bool = False
try:
import cv2
import numpy
_useOpenCV = True
except ImportError:
pass # This is fine, useOpenCV will stay as False.
然后根据
locateAll
设置 _useOpenCV
# set the locateAll function to use opencv if possible; python 3 needs opencv 3.0+
# TODO - Should this raise an exception if zero instances of the image can be found
# on the screen, instead of always returning a generator?
locateAll = _locateAll_pillow
if _useOpenCV:
locateAll = _locateAll_opencv
if not RUNNING_PYTHON_2 and cv2.__version__ < '3':
locateAll = _locateAll_pillow
opencv-python
软件包
尝试像在源代码中所做的那样导入它。
这就是我的代码使用 opencv 的样子:
import os
import pyautogui
pyautogui.FAILSAFE = False
pyautogui.moveTo(0, 0, duration=0)
# pyautogui.click(x=650, y=850, clicks=1, interval=0, button='left', duration=1) # Chrome
dirname = os.path.dirname(__file__)
img_path = os.path.join(dirname, "resources/chrome.png")
x, y = pyautogui.locateCenterOnScreen(img_path, confidence=0.7)
pyautogui.click(x=x, y=y, clicks=1, interval=0, button='left', duration=1)