appium截取手机长图(滚动截屏)

it2024-08-06  49

在做APP的UI自动化,想要通过简单的截取长图的方式,简单的对比下页面元素是否全部可见且无截断,所以写了个将appium截图进行拼接的脚本。各位可以自行优化。

from PIL import Image import os #打开2个图像 image1 = os.getcwd() +'/pics/1.png' image2 = os.getcwd() +'/pics/2.png' img_info1 = Image.open(image1) img_data1 = img_info1.getdata() img_info2 = Image.open(image2) img_data2 = img_info2.getdata() #获取第一张图最后一行和倒数第五行的像素信息 last_pix = [] for w in range(img_info1.width): last_pix.append(img_data1.getpixel((w, img_info1.height-1))) last_five_px = [] for w in range(img_info1.width): last_five_px.append(img_data1.getpixel((w, img_info1.height-6))) #寻找在第二张图中和第一张图最后一行相等的行 #如果相等行存在,去看往前数5行是否也相等 #这个判断可能要根据页面的复杂度多校验几次,约简单的图,越要多校验几行 row = 0 print('图片高度是:{}'.format(img_info2.height)) for h in range(img_info2.height): current = [] current_five = [] for w in range(img_info2.width): current.append(img_data2.getpixel((w, h))) try: current_five.append(img_data2.getpixel((w, h-5))) except Exception as e: print('处理h-5为空的情况') if current == last_pix and current_five == last_five_px: print('第{}行是一样的'.format(row)) break else: row =row + 1 #裁剪第二张图 print(img_info2.size) cropped2 = img_info2.crop((0, row+1, img_info2.width, img_info2.height)) cropped2.save(os.getcwd()+'/pics/temp.png') #拼接图片 newpic = Image.new('RGB', (img_info1.width, cropped2.height + img_info1.height)) newpic.paste(img_info1, (0, 0)) newpic.paste(cropped2, (0, img_info1.height+1)) newpic.save(os.getcwd()+'/pics/temp3.png')
最新回复(0)