python - 如何使用 pyautogui 处理图像
问题描述
我想知道是否可以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
解决方案
试试这个:
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))
这个问题与另一个问题相似,我在两个地方都发布了相同的答案。
推荐阅读
- python - 如何在 Tensorflow 数据集中获得类分布?
- powerbi - 我们可以在 Power BI 网关连接时使用 localhost 作为服务器名称吗?
- android - 如何在 json 响应中获取值
- google-apps-script - 评估/执行时谷歌电子表格自定义函数?
- python - 使用 Python 从“数据库浏览器”Wordpress 插件下载 CSV 文件
- python - 使用 Pandas (Python) 基于单个条件为多个列分配值
- c++ - 带有集成变量的c ++字符串
- c++ - 在 C++ 中动态存储锯齿状数组,具有锯齿状行但恒定列
- ssl - Kubernetes 证书管理器问题
- bash - 在几个 csv 文件上找到 xargs 粘贴