对于多个图像
一堆图像,您会如何做
pyautogui.locateOnScreen()
?
试试这个:
import os
import pyautogui as py
image_list = []
# Get list of all files in current directory
directory = os.listdir()
# Find files that end with .png or .jpg and add to image_list
for file in directory:
if file.endswith('.png') or file.endswith('.jpg'):
image_list.append(file)
# Loop through list to find all the images
for image in image_list:
print(image)
print(py.locateOnScreen(image))
这个问题与另一个一个类似,我在两个地方都发布了相同的答案。
def get_coordinates(images):
# Capture your screen only once.
haystack = pyautogui.screenshot()
# Loop through multiple images
for needle in images:
# Get coordinates of image within screen capture
return pyautogui.locate(needle, haystack)
print("Images not found!")
使用屏幕截图功能捕获屏幕,然后循环浏览不同的图像以查找匹配项。
要优化请参阅优化
@Afro_Jello 实施的改进版本。如果图像未显示,请提供帮助:
def get_coordinates(images, max_tries=3):
import pyautogui
from contextlib import suppress
# Resolve retries to wait the image been display
while max_tries:
max_tries -= 1
# Capture your screen only once.
haystack = pyautogui.screenshot()
# Loop through multiple images
for needle in images:
# Get coordinates of image within screen capture
with suppress(pyautogui.ImageNotFoundException):
return pyautogui.locate(needle, haystack)
sleep(1)
raise TimeoutError("Images not found!")