如何修复 pyautogui 的 locateOnScreen 找不到任何东西?
How do I fix the pyautogui's locateOnScreen not finding anything?
这是代码,
import pyautogui
import time
while 1:
if pyautogui.locateOnScreen("something.png", region=(0,400,575,400), confidence =0.6 ) != None:
print(" I see")
time.sleep(0.1)
else:
print("Me no see")
time.sleep(0.1)
无论我怎么努力,它只会一遍又一遍地打印“我看不见”。我尝试将置信度设置为 0.5,但这只会打印“我明白了”。
您可以为 pyautogui
找到关于 Screenshot Functions 的精彩文档。
confidence
参数是可选的,必须有 OpenCV
才能工作。
region
参数也是可选的,您可以将其删除,但仍会找到图像。
如果需要,可以使用参数 grayscale=True
。正如医生所说:This desaturates the color from the images and screenshots, speeding up the locating but potentially causing false-positive matches
.
唯一不可选的参数是图像,它需要是 .png
扩展名。
您还需要提供从屏幕上获取的相同分辨率和比例的图像。
假设您提供的图像 something.png
从浏览器中以 150%
缩放比例获取,并且还从 100%
缩放比例搜索此图像,它 不会 找到图像,无论您使用它的参数如何!
所以记得再次保存图片以便找到它们!
import pyautogui
if pyautogui.locateOnScreen("something.png", grayscale=True, confidence=0.8) != None:
print("I see")
这是代码,
import pyautogui
import time
while 1:
if pyautogui.locateOnScreen("something.png", region=(0,400,575,400), confidence =0.6 ) != None:
print(" I see")
time.sleep(0.1)
else:
print("Me no see")
time.sleep(0.1)
无论我怎么努力,它只会一遍又一遍地打印“我看不见”。我尝试将置信度设置为 0.5,但这只会打印“我明白了”。
您可以为 pyautogui
找到关于 Screenshot Functions 的精彩文档。
confidence
参数是可选的,必须有 OpenCV
才能工作。
region
参数也是可选的,您可以将其删除,但仍会找到图像。
如果需要,可以使用参数 grayscale=True
。正如医生所说:This desaturates the color from the images and screenshots, speeding up the locating but potentially causing false-positive matches
.
唯一不可选的参数是图像,它需要是 .png
扩展名。
您还需要提供从屏幕上获取的相同分辨率和比例的图像。
假设您提供的图像 something.png
从浏览器中以 150%
缩放比例获取,并且还从 100%
缩放比例搜索此图像,它 不会 找到图像,无论您使用它的参数如何!
所以记得再次保存图片以便找到它们!
import pyautogui
if pyautogui.locateOnScreen("something.png", grayscale=True, confidence=0.8) != None:
print("I see")