python 用于识别图像何时出现在屏幕区域中的脚本

python script to identify when an image is present in an area on the screen

所以我想让程序主动寻找我在屏幕上某个区域给它的图像。如果它在屏幕上,我想要代码 运行 ,如果它不在

上,我想要其他代码 运行

我一直在尝试使用 pyautogui 执行此操作,但它似乎不起作用。

pyautogui.locateCenterOnScreen('F.png', confidence = .7, region = (275, 259, 1011, 482))

当我打印这个但它没有找到输出的图像时:None 当我打印它时它确实找到了它将输出的图像:Point(x=297, y=266)

所以我设置了一个 if 语句

findImg = pyautogui.locateCenterOnScreen('F.png', confidence = .7, region = (275, 259, 1011, 482))

print (findImg)

if findImg == 'None':
   print ("not found")

这会打印 None 但不会打印 not found

我该如何解决这个问题或者有更好的方法吗?

这是因为 'None' 在引号中所以它是一个字符串,而不是 None。请尝试 if findImg == None:。尽管更好的方法可能是将 print(findImg) 移动到 else: 块,这样它就不会打印“None”和“未找到”

if findImg == None:
   print ("not found")
else:
   print(findImg)