我的螢屏someButton.png上可能會出現四個區域。我想指定這四個區域而不是整個螢屏。
我知道我們可以像下面的例子一樣指定單個區域,但檔案沒有說明多個區域。
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"
該檔案仍然存在,在回圈中傳遞區域元組是否會更改某些內容的格式?
uj5u.com熱心網友回復:
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安裝。如果您只有其中之一,或者沒有,它將回退到使用pillow.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/349127.html
下一篇:Java中的影像到模板
