应用场景:在屏幕中有某个图标,不通过查找句柄方式点击该图标

一、文件:selectImg.py

#查找坐标-依赖
import aircv
#点击-依赖
from pymouse import PyMouse
#截屏-依赖
from PIL import ImageGrab
import time

#-----------------------------------------【函数区域】--------------------------------
#在原始图片中找某个图标的位置
def matchImg(imgsrc,imgobj,confidencevalue=0.5):#imgsrc=原始图像,imgobj=待查找的图片
    imsrc = aircv.imread(imgsrc)
    imobj = aircv.imread(imgobj)
 
    match_result = aircv.find_template(imsrc,imobj,confidencevalue) 
    if match_result is not None:
        match_result['shape']=(imsrc.shape[1],imsrc.shape[0])#0为高,1为宽

    return match_result
#全屏截图
def Fullscreen():
    im = ImageGrab.grab()
    im.save('./Fullscreen.jpg')
    return './Fullscreen.jpg'

#-----------------------------------------【流程区域】--------------------------------
if __name__ == '__main__':
    #先进行截图
    imgsrc = Fullscreen()
    #设置要找的图标
    imgobj = "./7.png"
    #找到位置
    match_result = matchImg(imgsrc,imgobj)
    #得到位置的x和y
    xy = list(match_result['result'])
    #点击
    m = PyMouse()
    m.click(int(xy[0]), int(xy[1]))  #移动并且在(x,y)位置左击

    time.sleep(3)

    #先进行截图
    imgsrc = Fullscreen()
    #设置要找的图标
    imgobj = "./8.png"
    #找到位置
    match_result = matchImg(imgsrc,imgobj)
    #得到位置的x和y
    xy = list(match_result['result'])
    #点击
    m = PyMouse()
    m.click(int(xy[0]), int(xy[1]))  #移动并且在(x,y)位置左击

    time.sleep(3)

    #先进行截图
    imgsrc = Fullscreen()
    #设置要找的图标
    imgobj = "./9.png"
    #找到位置
    match_result = matchImg(imgsrc,imgobj)
    #得到位置的x和y
    xy = list(match_result['result'])
    #点击
    m = PyMouse()
    m.click(int(xy[0]), int(xy[1]))  #移动并且在(x,y)位置左击

    time.sleep(3)

    #先进行截图
    imgsrc = Fullscreen()
    #设置要找的图标
    imgobj = "./10.png"
    #找到位置
    match_result = matchImg(imgsrc,imgobj)
    #得到位置的x和y
    xy = list(match_result['result'])
    #点击
    m = PyMouse()
    m.click(int(xy[0]), int(xy[1]))  #移动并且在(x,y)位置左击

    time.sleep(3)

    #先进行截图
    imgsrc = Fullscreen()
    #设置要找的图标
    imgobj = "./11.png"
    #找到位置
    match_result = matchImg(imgsrc,imgobj)
    #得到位置的x和y
    xy = list(match_result['result'])
    #点击
    m = PyMouse()
    m.click(int(xy[0]), int(xy[1]))  #移动并且在(x,y)位置左击
最后修改:2023 年 12 月 05 日
如果觉得我的文章对你有用,请随意赞赏