画像にpyautoguiを使用する方法

Nov 28 2020

pyautogui.locateOnScreen()完全なフォルダに対して実行できるかどうか疑問に思っていました。これは、20の異なる画像を含むフォルダを意味し、画面上でそれらを見つけることを意味します。で行うことは可能pyautoguiですか?それともどうしますか?

これはこれまでの私のコードです:

from pyautogui import locateAllOnScreen as find
import os
import numpy as np




def try_to_find(x):
    x = os.path.isfile(x)
    if x == None:
        Warning('No images were enterd')   
        
    else:
        folder = x
        value = find(folder)
        
        if value is not None:
            print(f"{x} was found!")
            
        else:
            if value is None:
                print(f"{x} was not found!")
                
        return(list(value))



myfolder = ("ImageQuery")
found = 0

with os.scandir(myfolder) as entries:
    for entry in entries:
        if entry.is_file():
            found+=1
            print(f'Items {found}: {entry.name}')
            try_to_find(entry.name)

            

このコードを実行すると、このエラーが発生します TypeError: expected an image filename, OpenCV numpy array, or PIL image

回答

1 AndrewStone Nov 29 2020 at 01:56

これを試してみてください:

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))

この質問は、別のに似ている1、私は両方の場所で同じ答えを掲載しました。