zoukankan      html  css  js  c++  java
  • 【pyautogui】利用Python进行windows系统上的图像识别与点击(Mac OS系统也可以)

     系统环境:

    1、安装了python

    2、安装了pyautogui模块

    windows系统:无需安装依赖模块,在cmd中直接输入pip install pyautogui即可完成安装

    Mac OS系统:需要先安装pyobjc模块,最后安装pyautogui

    pip install pyobjc-core
    pip install pyobjc
    pip install pyautogui

    首先为python安装pyautogui模块

    Windows系统直接在cmd下输入pip install pyautogui

    验证是否安装成功

    在cmd下输入python,之后输入import pyautogui,再输入pyautogui.__version__

    操作步骤:

    第一步:将需要识别的图像用截图软件进行截图,例如截取windows系统上自带计算器上的数字8,保存文件命名为target.png

    第二步:书写代码,将代码与target.png放置在同一路径下

    import pyautogui
    #判定目标截图在系统上的位置
    location=pyautogui.locateOnScreen(image='target.png')
    #输出坐标
    print(location)
    #利用center()函数获取目标图像在系统中的中心坐标位置 x,y=pyautogui.center(location) print('center()',x,y) #对识别出的目标图像进行点击 #参数x,y代表坐标位置,clicks代表点击次数,button可以设置为左键或者右键 pyautogui.click(x=x,y=y,clicks=1,button='left')

     第三步:执行,需要注意的是,计算器必须在最上层,否则会导致无法识别。

     如下图,运行程序后直接点击了计算器上的8

     ps:若屏幕上有多个计算器的按钮8需要识别,需要将

    location=pyautogui.locateOnScreen(image='target.png')改为
    location=pyautogui.locateAllOnScreen(image='target.png')
    再利用循环点击
    
    
    import pyautogui
    #判定目标截图在系统上的位置
    location=pyautogui.locateAllOnScreen(image='target.png')
    #输出坐标
    
    for i in location:
    
        print(i)
    
        #利用center()函数获取目标图像在系统中的中心坐标位置
        x,y=pyautogui.center(i)
        print('center()',x,y)
    
        #对识别出的目标图像进行点击
        #参数x,y代表坐标位置,clicks代表点击次数,button可以设置为左键或者右键
        pyautogui.click(x=x,y=y,clicks=1,button='left')

     

     
  • 相关阅读:
    django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
    Error fetching command 'collectstatic': You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. Command 'collectstatic' skipped
    windows 虚拟环境下 安装 mysql 引擎一系列错误处理
    项目概念流程
    pip 使用
    HTTPserver v3.0 版本项目
    GitHub 使用
    git 操作命令详解
    git 忽略部分文件类型的同步
    Python 正则处理_re模块
  • 原文地址:https://www.cnblogs.com/ffrs/p/11353650.html
Copyright © 2011-2022 走看看