如何使用 pyautogui 指定多个区域?

How do I specify multiple regions with pyautogui?

我的屏幕上有四个区域 someButton.png 可能会出现。我想指定这 4 个区域而不是整个屏幕。

我知道我们可以像下面的例子一样指定单个区域,但是 documentation 没有说明多个区域。

    import pyautogui
    pyautogui.locateOnScreen('someButton.png', region=(0,0, 300, 400))

我试过添加多个关键字:

pyautogui.locateOnScreen('someButton.png', region=(0,0, 300, 400), region=(0,0, 400, 500))

但我收到错误 SyntaxError: keyword argument repeated: region

如何在pyautogui中指定多个区域进行搜索?

编辑** 评论更新**

我现在使用的代码:

def l():
    l = py.locateOnScreen(levelupimage, confidence=0.90)

    regions = {
        "region 1": (476, 268, 736, 320),
        "region 2": (1328, 268, 1591, 320),
        "region 3": (276, 745, 564, 806),
        "region 4": (1130, 745, 1422, 803)
    }

    for region in regions:
        l = py.locateOnScreen('levelup.jpg', region=region)
        if l != None:
            py.click(l)

错误信息:

Traceback (most recent call last):
  File "c:\Users\x\OneDrive\froggy-pirate-master\avoidShips\eventfarm\eventfarm.py", line 201, in <module>
    daily()
  File "c:\Users\x\OneDrive\froggy-pirate-master\avoidShips\eventfarm\eventfarm.py", line 191, in daily
    levelup()
  File "c:\Users\x\OneDrive\froggy-pirate-master\avoidShips\eventfarm\eventfarm.py", line 55, in levelup
    levelup = py.locateOnScreen('levelup.jpg', region=region)
  File "C:\Users\x\AppData\Local\Programs\Python\Python39\lib\site-packages\pyautogui\__init__.py", line 175, in wrapper
    return wrappedFunction(*args, **kwargs)
  File "C:\Users\x\AppData\Local\Programs\Python\Python39\lib\site-packages\pyautogui\__init__.py", line 213, in locateOnScreen
    return pyscreeze.locateOnScreen(*args, **kwargs)
  File "C:\Users\x\AppData\Local\Programs\Python\Python39\lib\site-packages\pyscreeze\__init__.py", line 373, in locateOnScreen
    retVal = locate(image, screenshotIm, **kwargs)
  File "C:\Users\x\AppData\Local\Programs\Python\Python39\lib\site-packages\pyscreeze\__init__.py", line 353, in locate
    points = tuple(locateAll(needleImage, haystackImage, **kwargs))
  File "C:\Users\x\AppData\Local\Programs\Python\Python39\lib\site-packages\pyscreeze\__init__.py", line 207, in _locateAll_opencv
    needleImage = _load_cv2(needleImage, grayscale)
  File "C:\Users\x\AppData\Local\Programs\Python\Python39\lib\site-packages\pyscreeze\__init__.py", line 170, in _load_cv2
    raise IOError("Failed to read %s because file is missing, "
OSError: Failed to read l.jpg because file is missing, has improper permissions, or is an unsupported or invalid format

l 正在函数 l = r"C:\Users\x\OneDrive\froggy-pirate-master\avoidShips\eventfarm\l.jpg"

上方加载

文件还在,在循环中传递区域元组会改变某些东西的格式吗?

regions = {
    "region 1": (0, 0, 300, 400),
    "region 2": (300, 0, 300, 400),
    "region 3": (0, 400, 300, 400),
    "region 4": (300, 400, 300, 400)
}
for region_name, region in regions.items():
    rect = pyautogui.locateOnScreen('someButton.png', region=region)
    if rect:
        print(f"found in {region_name} at this (x,y,w,h): {rect}")

我查看了源代码,由于在区域中查找图像的部分可能非常慢,与可能的图像位置呈线性关系,因此最好为每个区域分别调用 locateOnScreen (而不是在包含您的四个区域的区域上调用一次)。

阅读源代码看来,为了提高速度,您应该同时安装 cv2 (OpenCV) 和 numpy。如果您只有其中之一,或 none,它将退回到使用 pillow