这是我尝试运行的代码:
import pyautogui
r=pyautogui.locateOnScreen('C:\Users\David\Desktop\index.png',grayscale=False)
print r
答案 0 :(得分:6)
必须完全匹配像素才能找到。要允许任何形式的偏差,您可以调用一个置信度参数。
例如:
this.loadFeederNamesOnMultiSelect = function (feederNames) {
$("#multiSelectShowHideFeeders").kendoMultiSelect({
dataSource: feederNames,
autoClose: false,
deselect: that.onDeselect,
select: that.onSelect,
});
};
this.onDeselect = function (e) {
var feeders = $("#multiSelectShowHideFeeders").data("kendoMultiSelect").value();
that.showFeatures(that.shMap.primariesLayer, feeders);
};
this.onSelect = function (e) {
var feeders = $("#multiSelectShowHideFeeders").data("kendoMultiSelect").value();
that.hideFeatures(that.shMap.primariesLayer, feeders);
};
但是,为了使用confidence参数,您必须安装opencv_python。这很容易通过pip安装:
loc = pyautogui.locateOnScreen(image, grayscale=True, confidence=.5)
在此之后,您应该能够解决微小的差异。
答案 1 :(得分:2)
我遇到了同样的问题,我做的是
import pyautogui
r= None
while r is None:
r=pyautogui.locateOnScreen('C:\Users\David\Desktop\index.png',grayscale=False)
print r
我认为这只是因为定位图像需要时间。如果您找到了更好的解决方案与我共享:)
答案 2 :(得分:1)
如果在屏幕上找不到图像,locateOnScreen()
函数将返回None
。请记住,匹配必须是像素完美才能与之匹配,因此请务必将index.png裁剪为最小的可识别大小,以防止额外的细节破坏您的匹配。此外,请确保您要查找的内容不会被其上任何其他窗口遮挡。
答案 3 :(得分:1)
我使用以下方法实现了这项工作:
r = None
while r is None:
r = pyautogui.locateOnScreen('rbin.PNG', grayscale = True)
print icon_to_click + ' now loaded'
关键是要使灰度=真。
答案 4 :(得分:1)
我遇到了同样的问题,一直返回 None 值。
我进行了多次试验并找到了适合我的解决方案。 操作系统:MacOS
我用我的系统截图工具(command+shift+5)保存了照片并保存。它似乎与我的屏幕上显示的像素信息不同。 因此我使用 pyautogui 截图来保存我想要的照片。
pyautogui.screenshot('num7_.png', region=(260,360, 110, 100))
此后,无论灰度参数如何,它都可以正常工作。
pyautogui.locateOnScreen('num7_.png')
Box(left=260, top=360, width=110, height=100)
答案 5 :(得分:0)
我遇到了这个问题,但后来我将照片裁剪成特定的部分然后找到了,是的,这需要时间。
或者这也可以。
b = pyautogui.center('calc7key.png')
答案 6 :(得分:0)
我找到了解决问题的方法。仅搜索尽可能小的图像。在3秒后找到仅1个像素的图片。当我尝试搜索超过500x500的图像时,它将找不到任何东西。
答案 7 :(得分:0)
答案 8 :(得分:0)
我有类似的问题。
我的错是我先将比较图片另存为jpg,然后在MS paint中另存为png。
确保将比较图片保存为png格式。之后,“定位”功能对我有用。
答案 9 :(得分:0)
documentation官员说;
The Locate Functions
NOTE: As of version 0.9.41, if the locate functions can’t find the provided image,
they’ll raise ImageNotFoundException instead of returning None.
因此,您可以决定是否引发异常。另外,您应该尝试一定次数而不是While True
循环。
retry_counter = 0
while retry_counter < 5:
try:
result = pyautogui.locateOnScreen(IMAGE_PATH_TO_FIND)
if result:
time.sleep(1)
retry_counter = 10 # to break the loop
except:
time.sleep(1) # retry after some time, i.e. 1 sec
retry_counter += 1
答案 10 :(得分:0)
我的问题是我想剪一下计算器按钮。那必须使像素匹配不同,因为我在这里尝试了其他所有选项,但没有任何效果。我做了一个打印屏幕,然后将其裁剪到想要的按钮上,就可以了。
答案 11 :(得分:0)
第一个答案就是我的答案。
完成pip之后,安装opencv_python,然后像下面的工作一样修改我的搜索:
button7location = pyautogui.locateOnScreen('1606271188298.png',grayscale=True, confidence=.5)
谢谢!
答案 12 :(得分:0)
之前
import pyautogui
image = '9.png'
loc = pyautogui.locateOnScreen(image, grayscale=True, confidence=.5)
print (loc)
错误:
None
>>>
解决方案
import pyautogui
import time
time.sleep(5)
image = '9.png'
loc = pyautogui.locateOnScreen(image, grayscale=True, confidence=.5)
print (loc)
总结:只需添加这两行:
import time
time.sleep(5)
输出
Box(left=1686, top=248, width=70, height=47)
>>>
import time
time.sleep(5)
答案 13 :(得分:-1)
如果您使用截图工具截取了屏幕截图,它将无法工作,因此请使用“prt sc”或命令提示符进行截图。这对我有用!