【Python】【Appium】截图 对指定元素(element)截图 PIL&Pillow

 

"""
PIL下载:https://www.lfd.uci.edu/~gohlke/pythonlibs/
Pillow‑8.0.1‑cp39‑cp39‑win_amd64.whl:Python3.9 Windows 64位
"""

from appium import webdriver
from PIL import Image

    @staticmethod
    def screenshot_by_element(driver, element, out_image: str):
        """
        对指定元素截图
        :param driver:
        :param element: 元素
        :param out_image: 截图输出路径(验证码.png)
        """
        # step1 全屏图
        global_image = '全屏图.png'
        driver.get_screenshot_as_file(global_image)

        # step2 获取元素的四个坐标
        min_x = element.location['x']
        min_y = element.location['y']
        max_x = min_x + element.size['width']
        max_y = min_y + element.size['height']

        # step3 从全屏图中裁剪出目的元素的图片
        im = Image.open(global_image)
        im = im.rotate(-90, expand=True)  # 若需要旋转图片 则执行这句 顺时针方向旋转90度(负数:顺时针;正数:逆时针)
        im.save(global_image)
        im = Image.open(global_image)
        im = im.crop((min_x, min_y, max_x, max_y))  # 裁剪(左上至右下)
        print(min_x, min_y, max_x, max_y)
        im.save(out_image)

 

posted @ 2020-11-11 17:08  淡怀  阅读(1132)  评论(0编辑  收藏  举报