Airtest局部找图

airtest局部找图

代码作为参开,在自己的项目中就修改吧

# -*- encoding=utf8 -*-
__author__ = "Administrator"

from airtest.core.api import *
from airtest.cli.parser import cli_setup

from airtest.aircv import *
import time


def FindSubImg(temp, endPos, startPos):
    """
    局部找图
    :param startPos:        找图范围开始点
    :param endPos:          找图范围结束点
    :param temp:            目标图片
    :return: [posX,posY] | None    坐标
    """
    startTime = int(round(time.time() * 1000))
    endTime = int(round(time.time() * 1000))
    print('Log SubTime', endTime - startTime)
    loopFlag = 0
    while endTime - startTime < 3000:
        loopFlag += 1

        print('Log loopFlag', loopFlag)

        # 全屏截图
        screen = G.DEVICE.snapshot()

        # 获取设备屏幕分辨率(竖屏)
        height = G.DEVICE.display_info['height']
        width = G.DEVICE.display_info['width']
        print('Log height', height)
        print('Log width', width)

        # 局部截图
        local_screen = aircv.crop_image(screen, (startPos[0], startPos[1], endPos[0], endPos[1]))

        # 将我们的目标截图设置为一个Template对象
        tempalte = temp
        # 在局部截图里面查找指定的图片对象
        pos = tempalte.match_in(local_screen)
        print('相对坐标', pos)

        if not pos:
            endTime = int(round(time.time() * 1000))
            print('所用时间', endTime - startTime)
            continue

        # 返回找到的图片对象的坐标(该坐标是相对于局部截图的坐标)
        # print(pos)

        # 若要返回目标在整个屏幕中的坐标,则x,y都需要加上局部截图时设置的最小x、y
        resPos = [pos[0] + startPos[0], pos[1] + startPos[1]]
        print('绝对坐标', resPos)
        endTime = int(round(time.time() * 1000))
        print('所用时间', endTime - startTime)
        return resPos

   
    endTime = int(round(time.time() * 1000))
    print('耗时', endTime - startTime)
    # Python中的exists函数用于检查文件或目录是否存在。
   
    return exists(temp)


# 在一个函数中可以以多个return语句,只有条件成立的第一个return会被执行到并返回相应的值。
print("\n\n\n=== 开始 ===\n\n\n")

touch(FindSubImg(Template(r"tpl1623399241628.png", target_pos=5, record_pos=(-0.426, -0.904), resolution=(1080, 2340)), [350, 340],[750, 1200]))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大数据采集及分析RPA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值