python等待一个图片出现

        在RPA的操作过程中,经常会出现等待一个目标出现,我这里提供一个python图片等待的方法。目的是等待一个图片或目标的出现。

        首先先准备环境,我将我用到的所有包及版本贴在下面,大家自行pip

echo =============install pywin32==300==============
pip install pywin32-300-cp38-cp38-win_amd64.whl


echo =============install numpy==1.18.4==============
pip install numpy-1.18.4-cp38-cp38-win_amd64.whl


echo =============install Pillow==8.1.0==============
pip install Pillow-8.1.0-cp38-cp38-win_amd64.whl


echo =============install opencv_python==4.4.0.46==============
pip install opencv_python-4.4.0.46-cp38-cp38-win_amd64.whl


echo =============install PyTweening==1.0.3==============
pip install PyTweening-1.0.3.zip


echo =============install pyscreeze==0.1.26==============
pip install PyScreeze-0.1.26.tar.gz


echo =============install pyRect==0.1.4==============
pip install PyRect-0.1.4.tar.gz


echo =============install pygetwindow==0.0.9==============
pip install PyGetWindow-0.0.9.tar.gz


echo =============install pyperclip==1.8.2==============
pip install pyperclip-1.8.2.tar.gz


echo =============install mouseinfo==0.1.3==============
pip install MouseInfo-0.1.3.tar.gz


echo =============install pyMsgbox==1.0.9==============
pip install PyMsgBox-1.0.9-py3-none-any.whl


echo =============install pyautogui==0.9.52==============
pip install PyAutoGUI-0.9.52.tar.gz


echo =============install pbr==5.3.0==============
pip install pbr-5.3.0-py2.py3-none-any.whl


echo =============install aircv==1.4.6==============
pip install aircv-1.4.6.tar.gz

然后用到的方法代码直接放在下面,大家可以直接调用

import os
from os.path import dirname, abspath, split
import sys
currPath = abspath(dirname(__file__))
sys.path.append(split(currPath)[0])
from time import sleep
import aircv as ac
import cv2
import pyautogui as pyGui
from PIL import ImageGrab as ImgGrab
import datetime
import numpy

    def picWait(picPath, screenPicDir, sec=30, similarity=0.95, picExtension = ".png"):
        """
        等待某个图片区域出现
        :param picPath: 图片名称
        :param screenPicDir: 屏幕截图路径
        :param sec: 等待超时时间,默认30s
        :param similarity: 相似度,默认0.85
        :param picExtension: 文件名后缀,要求是.png
        :return: 定位到则返回match,否则抛出异常
        """
        # if sec is None:
        #     sec = waitSec
        # if similarity is None:
        #     similarity = clzBase.similarity

        if similarity is None:
            similarity = similarity
        # picDir = os.path.join(self.picBaseDir, folderName)
        if not os.path.isfile(picPath):
            raise Exception("图片路径不存在:picPath={}".format(picPath))
        elif not str(picPath).lower().endswith(picExtension):
            raise Exception("图片对象后缀名错误:picExtension={},需.png类型".format(picExtension))
        # picInfo = ac.imread(picPath)
        # screenInfo = ac.imread(screenPicPath)
        width, height = pyGui.size()
        img = ImgGrab.grab(bbox=(0, 0, width, height))
        if not os.path.isdir(screenPicDir):
            os.makedirs(screenPicDir)
        screenPicPath = os.path.join(screenPicDir, "screen" + picExtension)
        if os.path.isfile(screenPicPath):
            os.remove(screenPicPath)
        img.save(screenPicPath)
        picInfo = cv2.imdecode(numpy.fromfile(picPath, dtype=numpy.uint8), -1)
        screenInfo = cv2.imdecode(numpy.fromfile(screenPicPath, dtype=numpy.uint8), -1)
        match = ac.find_template(screenInfo, picInfo, float(similarity))
        waitSec = int(sec)
        while (not match) and waitSec > 0:
            # logging.info("第{}次定位失败,剩余{}次".format(secConst - waitSec + 1, waitSec - 1))
            sleep(0.9)
            if similarity is None:
                similarity = similarity
            # picDir = os.path.join(self.picBaseDir, folderName)
            if not os.path.isfile(picPath):
                raise Exception("图片路径不存在:picPath={}".format(picPath))
            elif not str(picPath).lower().endswith(picExtension):
                raise Exception("图片对象后缀名错误:picExtension={},需.png类型".format(picExtension))
            # picInfo = ac.imread(picPath)
            # screenInfo = ac.imread(screenPicPath)
            width, height = pyGui.size()
            img = ImgGrab.grab(bbox=(0, 0, width, height))
            if not os.path.isdir(screenPicDir):
                os.makedirs(screenPicDir)
            screenPicPath = os.path.join(screenPicDir, "screen" + picExtension)
            if os.path.isfile(screenPicPath):
                os.remove(screenPicPath)
            img.save(screenPicPath)
            picInfo = cv2.imdecode(numpy.fromfile(picPath, dtype=numpy.uint8), -1)
            screenInfo = cv2.imdecode(numpy.fromfile(screenPicPath, dtype=numpy.uint8), -1)
            match = ac.find_template(screenInfo, picInfo, float(similarity))
            waitSec = waitSec - 1

        if (not match) and waitSec <= 0:
            errInfo = "定位图片{}秒超时失败:picPath = {}".format(os.path.abspath(picPath),picPath)
            raise Exception(errInfo)
        else:
            return match

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值