zoukankan      html  css  js  c++  java
  • python裁剪base64编码的图片

    简介

    今天遇到需要裁剪base64字符串的PNG图片,并返回base64格式字符串的任务,捣鼓半天。
    裁剪代码如下:

    def deal_inspect_img(base64_str):
        """裁剪base64字符串的图片"""
        base64_data = re.sub('^data:image/.+;base64,', '', base64_str)
        byte_data = base64.b64decode(base64_data)
        # BytesIO 对象
        image_data = io.BytesIO(byte_data)
        # 得到Image对象
        img = Image.open(image_data)
        # 裁剪图片(左,上,右,下),笛卡尔坐标系
        img2 = img.crop((962, 485, 1897, 810))
    
        # BytesIO 对象
        imgByteArr = io.BytesIO()
        # 写入BytesIO对象
        img2.save(imgByteArr, format='PNG')
        # 获得字节
        imgByteArr = imgByteArr.getvalue()
        base64_str = base64.b64encode(imgByteArr)
        return base64_str
    

    如果需要保存

    base64_str = 'xxxxxxxxxxxxxxxxxxxxxxx'
    imgdata = base64.b64decode(base64_str)
    with open('iss.png', 'wb') as f:
        f.write(imgdata)
    
  • 相关阅读:
    slf4j + log4j 记录日志
    Executors介绍
    Java集合(JDK1.6.0_45)
    Java多线程系列目录(共43篇)
    线程池体系
    FutureTask
    23种设计模式分类
    结构型 之 适配器模式
    信号量Semaphore详解以及应用
    Excel格式转化工具
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/10691085.html
Copyright © 2011-2022 走看看