在确定是否找到图像之前,如何遍历整个图像数组?

How do I iterate over an entire array of images before deciding if an image was found?

我想遍历整个图像数组,如果找到其中任何一个,我想单击 x

如果我到达数组的末尾并找到其中 none 个,我想单击 y 然后跳出循环。

我不知道如何遍历整个数组;这行得通,但它会按顺序遍历图像,检查是否匹配,如果不匹配,它会立即跳出循环,而不会检查更多图像。

如何检查数组中的所有图像是否匹配,然后在找到 none 时拆分?

for image in image_list:
    found = pyautogui.locateCenterOnScreen(image)
    if Found != None:                                                    
            pyautogui.click(x)                                
    else:
            pyautogui.click(y)
            break

使用评论中的详细信息更新了完整的工作代码。

import os
import pyautogui as py
from PIL.ImageOps import grayscale

a = 0
aC = 0
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'):
        image_list.append(file)

while True:
        if py.locateOnScreen('a.jpg') != None:
                breaking_bool = False
                while breaking_bool is False:
                    #Main Find Loop
                    for image in image_list:
                        name = image
                        Found = py.locateCenterOnScreen(image)
                        if Found != None:

                            py.moveTo(1700,1000,0.1)
                            py.sleep(0.01)
                            py.click()
                            py.sleep(1)
                            break
                        else:
                            py.moveTo(1415,1000,0.1)
                            py.sleep(0.01)
                            py.click()
                            py.sleep(1)
                            breaking_bool = True
                            
                    aC = aC + 1
                        
        a = a + 1
        

您需要跟踪是否找到了任何东西。现在您无法跟踪是否至少找到了一张图像。根据您的要求,我认为这会满足您的要求:

found_any = False
for image in image_list:
    found = pyautogui.locateCenterOnScreen(image)
    if found is not None:
        found_any = True
        pyautogui.click(x)
if not found_any:
    pyautogui.click(y)

如果找到图片,将为每个找到的图片单击 x。如果没有找到图像,将单击 y。

如果您只想单击 x 一次,那么您的代码将如下所示:

found_any = False
for image in image_list:
    found = pyautogui.locateCenterOnScreen(image)
    if found is not None:
        found_any = True
if not found_any:
    pyautogui.click(y)
else:
    pyautogui.click(x)

这是 for-else 循环的情况。

当循环运行时没有遇到 break 语句时计算 else 块。

for image in image_list:
    found = pyautogui.locateCenterOnScreen(image)
    if found != None:
        pyautogui.click(x)
        break
else:
    pyautogui.click(y)