import pyautogui
import cv2

# 图片列表,包含五张要识别的图片路径
image_list = ['image1.png', 'image2.png', 'image3.png', 'image4.png', 'image5.png']

while True:
    # 遍历图片列表
    for image_path in image_list:
        # 从屏幕上寻找指定图片
        image_location = pyautogui.locateOnScreen(image_path)
        
        # 如果找到图片
        if image_location is not None:
            # 获取图片中心点坐标
            image_center = pyautogui.center(image_location)
            x, y = image_center
            
            # 点击图片
            pyautogui.click(x, y, button='left')
            
    # 如果没有找到任何图片,暂停一段时间后继续下一轮循环
    pyautogui.time.sleep(1)

使用方法:

  1. 确保已安装 pyautoguiopencv-python 库: pip install pyautogui opencv-python
  2. 将五张要识别的图片命名为 'image1.png' 到 'image5.png' 并保存在相同的目录下。
  3. 运行脚本。

脚本功能:

  • 该脚本会无限循环地尝试在屏幕上识别预设的五张图片。
  • 一旦识别到图片,就会自动点击图片中心。
  • 如果未找到任何图片,脚本会暂停一秒后继续下一轮循环。
  • 可以通过手动停止脚本执行来结束循环。
Python自动化: 使用pyautogui和OpenCV实现循环图片识别点击

原文地址: https://www.cveoy.top/t/topic/V9y 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录