为什么在 pyautogui 中定位所有屏幕会返回多个相似的结果?

问题描述

我几乎通读了所有现有文档,但找不到答案。

这里是简单的代码

for pos in pyautogui.locateallOnScreen('image.png',confidence=0.9,grayscale=False):
print(pos)

我期待它找到这张图片的 3 个不同位置。 然而,结果是:

Box(left=1199,top=543,width=52,height=56)
Box(left=1200,height=56)
Box(left=1201,height=56)
Box(left=1198,top=544,height=56)
Box(left=1199,height=56)
Box(left=1202,top=545,height=56)
Box(left=821,top=853,height=56)
Box(left=820,top=854,height=56)
Box(left=822,top=855,top=1483,height=56)
Box(left=1203,height=56)
Box(left=1204,top=1484,height=56)

我如何告诉它显示至少“x”个像素远的结果?

顺便说一句,更改置信度分数无济于事。它要么是多个,要么没有。

解决方法

我目前没有安装 pyautogui,但在过去几年里安装了,所以我知道它在做什么。认为您需要将位置与已找到的位置进行比较,然后仅在找到大于您选择的阈值的位置时才添加它们。我不知道他们是否有内置功能,你必须看看。无论哪种方式,它基本上都会做这样的事情:

positions = []
threshhold = 8

for p in pyautogui .locateAllOnScreen( 'image.png',confidence=0.9,grayscale=False ):
    for pos in positions:
        if abs( pos['left'] -p['left'] ) > threshhold \
        and abs( pos['top'] -p['top'] ) > threshhold:
            positions .append( pos )

for pos in positions:
    print( pos )