uiautomator2 截图+压缩图片+放入allure报告中

发布时间 2023-08-25 15:58:25作者: Receiver
    def save_screenshot(self, screenshot_path):
        """
        截图保存到某个路径
        :param screenshot_path:
        :return:
        """
        self.d.screenshot(screenshot_path)

 

                screenshot_path = f"screenshot_wait.png"
                self.save_screenshot(screenshot_path)
                im = Image.open(screenshot_path)
                (x, y) = im.size  # 读取图片尺寸(像素)
                x_1 = 100  # 定义缩小后的标准宽度
                y_1 = int(y * x_1 / x)  # 计算缩小后的高度
                out = im.resize((x_1, y_1))  # 改变尺寸,保持图片高品质
                # 判断图片的通道模式,若图片在RGBA模式下,需先将其转变为RGB模式
                if out.mode == 'RGBA':
                    # 转化为rgb格式
                    out = out.convert('RGB')
                # 最后保存为jpg格式的图片,这里因为图片本身为jpg所以后缀不更改
                out.save(screenshot_path)
                # 将截图附加到Allure报告中
                with open(screenshot_path, "rb") as f:
                    allure.attach(f.read(), name=f"闪屏等待截屏1", attachment_type=allure.attachment_type.PNG)
                    self.sleep(0.4)
                delete(screenshot_path)
                self.sleep(0.1)
def delete(image_path):
    """
    删除图片
    :param image_path:
    :return:
    """
    if os.path.isfile(image_path):
        os.remove(image_path)

 

压缩图片 参考:https://www.jb51.net/article/268727.htm