使用PyAutoGUI识别PNG图像并自动点击按钮

在自动化测试、任务批处理等场景中,我们常常需要控制GUI程序的鼠标键盘操作。PyAutoGUI就是一个非常方便的Python模块,可以帮助我们实现这些操作。今天我们就来看看如何使用PyAutoGUI识别屏幕上的PNG图像,并自动点击图像所在位置。
C:\pythoncode\new\autoguirecongnizepng.py

全部代码:

import pyautogui
import cv2
import time


pyautogui.hotkey('win', 'r')
pyautogui.write('msedge')
pyautogui.press('enter')
 
# Go to bing.com
time.sleep(5)
 
pyautogui.hotkey('ctrl', 'l') 
 
pyautogui.write('http://localhost:44471/Forguncy')
pyautogui.press('enter')
pyautogui.press('enter')

time.sleep(5)
# 加载PNG图像
button_img = cv2.imread('button.png')

# 在屏幕上查找图像
button_location = pyautogui.locateOnScreen(button_img, confidence=0.8)

# 如果找到图像,点击其中心
if button_location is not None:
    button_x, button_y = pyautogui.center(button_location)
    pyautogui.click(button_x, button_y)
else:
    print('未找到按钮图像')

安装依赖库

在开始之前,我们需要先安装PyAutoGUI和OpenCV两个Python库:

pip install pyautogui
pip install opencv-python

PyAutoGUI用于控制鼠标键盘,而OpenCV则用于读取和处理图像。

导入模块

接下来在Python代码中导入必要的模块:

import pyautogui
import cv2

加载待识别图像

使用OpenCV读取待识别的PNG图像文件:

button_img = cv2.imread('button.png')

将图像路径替换为你自己的PNG文件路径。

在屏幕上查找图像

使用PyAutoGUI的locateOnScreen函数搜索与图像匹配的屏幕区域:

button_location = pyautogui.locateOnScreen(button_img, confidence=0.8)

confidence参数设置了匹配度阈值,范围0到1,值越高要求越精确。

点击图像中心

如果locateOnScreen成功找到了匹配区域,它会返回该区域的左上角坐标。我们可以计算出中心位置,并使用click函数在该位置模拟鼠标点击:

if button_location is not None:
    button_x, button_y = pyautogui.center(button_location)
    pyautogui.click(button_x, button_y)
else:
    print('未找到按钮图像')

完整代码

import pyautogui
import cv2

button_img = cv2.imread('button.png')
button_location = pyautogui.locateOnScreen(button_img, confidence=0.8)

if button_location is not None:
    button_x, button_y = pyautogui.center(button_location)
    pyautogui.click(button_x, button_y)
else:
    print('未找到按钮图像')

结果如下:
在这里插入图片描述

就是这样,使用PyAutoGUI和OpenCV我们可以很轻松地识别屏幕上的图像并执行点击操作。在实际使用中,你可能需要根据具体情况调整confidence参数以获得理想的匹配效果。另外注意,PyAutoGUI在运行时会直接控制鼠标键盘,所以测试时请小心操作。

希望这篇博客能够对你有所启发,如有任何疑问欢迎留言讨论。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值