Airtest测试分辨图片颜色

测试目的:判断当前步骤截取的图片上方是否大部分为绿色

代码:

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

from airtest.core.api import *

auto_setup(__file__)

from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)

from os import getcwd
from PIL import Image

# 获取当前工作目录
pic_path = getcwd()
# 截图并将图片保存到当前工作目录
snapshot(filename=f"{pic_path}/PicColor.png", msg="获取图片上方颜色")
# 读取图片并获取图片尺寸
img = Image.open(f"{pic_path}/PicColor.png")
width, height = img.size
# 处理图片
top_color = []
# 获取图片上方的RGB值,并将绿色范围内的收集进入列表
for colu in range(0, width):
    bar_color = img.getpixel((colu, 0.03*height))
    print(bar_color)
    if bar_color[0] < 20 and 245<= bar_color[1] <= 255 and bar_color[2] < 20:
        top_color.append(bar_color)
# 计算绿色的占比
green_rate = len(top_color)/width
print(green_rate)
if green_rate > 0.5:
    print('pass')

输出:

(2, 250, 3)
(4, 250, 3)
(0, 248, 0)
(2, 255, 6)
(5, 255, 9)
(7, 251, 5)
(21, 248, 8)
(16, 249, 7)
(0, 255, 2)
(0, 255, 11)
0.9972222222222222
pass

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值