pyautogui.locateOnScreen() 用于多张图片?

pyautogui.locateOnScreen() for multiple images?

对于 一堆 多张图片,您会如何 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!")

使用屏幕截图功能捕获您的屏幕,然后循环遍历不同的图像以找到匹配项。

优化见