栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【python】图片、二进制、base64转换

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【python】图片、二进制、base64转换

import base64

def image_to_byte(imagepath):
    '''
    图片转二进制
    '''
    with open(imagepath,"rb") as f:
        byte_data = f.read()
    return byte_data

def byte_to_image(byte_data,imagepath):
    '''
    二进制转为图片
    imagepath:图片保存路径
    '''
    with open(imagepath, "wb") as f:
        f.write(byte_data)

def image_to_base64(imagepath):
    '''
    图片转base64
    '''
    with open(imagepath,"rb") as f:
        base64_data = base64.b64encode(f.read()).decode("utf8")  # 读取文件内容,转换为base64编码
    return base64_data

def base64_to_image(base64_data,imagepath):
    '''
    base64转为图片
    imagepath:图片保存路径
    '''
    with open(imagepath, "wb") as f:
        f.write(base64.b64decode(base64_data))

if __name__ == "__main__":
    # res = image_to_byte(r'E:AttachmentsNN7469.png')
    # print(res)

    res = image_to_base64(r'E:AttachmentsNN7469.png')
    print(res)
    # picpath = r'E:AttachmentsMM7469.png'
    # base64_to_image(res,picpath)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/879229.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号