我正在尝试使用locateOnWindow()来定位图像的坐标
position = pt.locateOnWindow(image, 'Discord', grayscale=True, confidence=.9)
引发 PyGetWindowException('来自 Windows 的错误代码: %s - %s' % (errorCode, _formatMessage(errorCode))) pygetwindow.PyGetWindowException:来自 Windows 的错误代码:0 - 操作成功完成。
从
pyautogui
文档中,如 Screenshot Functions 所说:您可以调用 locateOnScreen()
函数来获取屏幕坐标。返回值是一个 4 整数元组:(左、上、宽、高)。该元组可以传递给 center()
函数来获取该区域中心的 X
和 Y
坐标。
所以我从笔记本电脑中保存了一个不和谐的图标图像,并找到了正确的坐标,如下所示:
图片:
import pyautogui
image = 'discord.png'
locate = pyautogui.locateOnScreen(image, grayscale=True, confidence=0.8)
position = pyautogui.center(pyautogui.locateOnScreen(image, grayscale=True, confidence=0.8))
如果您
print(locate)
:
Box(left=91, top=288, width=23, height=26)
如果您
print(position)
:
Point(x=102, y=301)
我遇到了和OP同样的问题。使用locateOnScreen('abc.png',confidence = 0.8)它可以工作,但是使用LocateOnWindow('abc.png','Window Title',confidence = 0.8)它不能 - 但窗口放大了,我的png 40x40像素