首页 > 解决方案 > pyautogui 在照片中多次搜索照片

问题描述

虽然在屏幕截图中不止一次找到了搜索过的“aaa.png”照片,但它只找到了 1 个。我希望它找到所有这些。我能为它做些什么

iml = pyautogui.screenshot(region=(158, 284, 628, 679))
while True:
    if pyautogui.locateOnScreen('aaa.png', confidence=0.9, grayscale=True) != None:
        for a in pyautogui.locateOnScreen('aaa.png'):
            print(a)

输出:

407
302
81
85
...

截图https://i.stack.imgur.com/aHDB6.png

aaa.png https://i.stack.imgur.com/Dow8f.png

标签: pythonpyautogui

解决方案


您想用它locateAllOnScreen来查找所有实例。

for pos in pyautogui.locateAllOnScreen('someButton.png')
   print(pos)

[更新重新评论]

我不能使用“截图”功能,但这个程序:

import pyautogui
res = pyautogui.locateAll('Dow8f.png', 'aHDB6.png',confidence=0.95, grayscale=True)
for a in res:
    print(a)

给了我 25 次点击,例如:

Box(left=9, top=584, width=81, height=85)
Box(left=115, top=584, width=81, height=85)
Box(left=222, top=584, width=81, height=85)
Box(left=541, top=584, width=81, height=85)

[你没有说你使用的是哪个版本的pyautogui;如果没有来自 的匹配项,最新版本将不会返回“无” locateOnScreen,所以也许您有旧版本?]

pyautogui Github 中的这个问题可能是相关的。默认confidence级别可能太挑剔了,所以使用我上面显示的值。]


推荐阅读