本文介绍了如何对图像使用 pyautogui的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以对一个完整的文件夹执行 pyautogui.locateOnScreen() 这就是我的意思是一个包含 20 个不同图像并在屏幕上找到它们的文件夹.是否可以使用 pyautogui?否则你会怎么做?

I was wondering if it was possible to do pyautogui.locateOnScreen() for a full folder this is what i mean a folder with 20 different images and finding them on the screen. Is it possible to do with pyautogui? or else how would you do it?

这是我目前的代码:

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

i get this error if i run this code TypeError: expected an image filename, OpenCV numpy array, or PIL image

推荐答案

试试这个:

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

这个问题类似于另一个一个,我在两个都发布了相同的答案地点.

This question is similar to another one, I posted the same answer in both places.

这篇关于如何对图像使用 pyautogui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 18:55