使用PyAutoGUI识别PNG图像并自动点击按钮 您所在的位置:网站首页 confident如何记忆 使用PyAutoGUI识别PNG图像并自动点击按钮

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

2024-07-09 14:20| 来源: 网络整理| 查看: 265

在自动化测试、任务批处理等场景中,我们常常需要控制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在运行时会直接控制鼠标键盘,所以测试时请小心操作。

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



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有