对于 locateOnScreen,PyAutoGui 总是 returns none

PyAutoGui always returns none for locateOnScreen

我的 pyautogui 总是 returns none 用于图像。图像与程序位于同一文件夹中。图像名称与我将它们保存的名称相同。这些图像是最新的,并且肯定在我的屏幕上。请帮助,pyautogui 总是 returns none 用于 locateOnScreen。这是我的代码:

import time
import sys
import pyautogui

pyautogui.FAILSAFE = True
pyautogui.PAUSE = 1
pyautogui.size()

width, height = pyautogui.size()


y = pyautogui.locateOnScreen('LOLicon.png')
print(y)
for i in range(2):
    x = pyautogui.moveTo(y)
    pyautogui.click(x)
    time.sleep(2)
    pyautogui.doubleClick()

del x
del y
import time
import sys
import pyautogui

pyautogui.FAILSAFE = True
pyautogui.PAUSE = 1
pyautogui.size()

width, height = pyautogui.size()

y = pyautogui.locateOnScreen('LOLicon.png')
print(y)
for i in range(2):
    x = pyautogui.moveTo(y[0:2])
    pyautogui.click(x)
    time.sleep(2)
    pyautogui.doubleClick()

上面的这个很完美。代码中唯一的变化是 x = pyautogui.moveTo(y[0:2])

因此请确保 当您 运行 您的脚本 时图像确实在屏幕上(未被代码编辑器或其他 window 隐藏) LOLicon.png 的图像内容实际上是您假设的

检查此代码是否也有帮助:

import pyautogui
im = pyautogui.screenshot(region=(20, 20, 50, 50)) 
im.save("myScreenshot.png")
y = pyautogui.locateOnScreen("myScreenshot.png")
print(y)
x = pyautogui.moveTo(y[0:2])

运行正常,没有错误。如果是这样,您可以将 myScreenshot.png 重命名为 LOLicon.png 并调整 region=(20, 20, 50, 50) 以使其捕获屏幕上的 LOLicon。

查看 https://pyautogui.readthedocs.io/en/latest/screenshot.html?highlight=save%20image 以了解 pyautogui 中的屏幕截图功能的详细信息,并确保安装了所需的模块(如果你在 Linux 上,则为 Pillow 和最终 scrot)。