博客
关于我
python 将图片与字符串相互转换
阅读量:797 次
发布时间:2023-03-07

本文共 507 字,大约阅读时间需要 1 分钟。

将图片转换为Base64编码的方法如下:方法一:打开图片文件:`with open(image,'rb') as f:`  读取图片内容并进行Base64编码:`str=base64.b64encode(f.read())`  打印编码结果:`print(type(str))`方法二:打开图片文件:`f=open(image,'rb')`  读取图片内容并进行Base64编码:`f_str=base64.b64encode(f.read())`  关闭文件:`f.close()`  打印编码结果:`print(type(f_str))`将Base64编码转换回图片:打开新文件:`file_str=open('2.jpg','wb')`  将编码内容写入文件:`file_str.write(base64.b64decode(str))`  关闭文件:`file_str.close()`

 

说明:本文展示了两种将图片文件转换为Base64编码的方法,并提供了将编码后的内容解码并保存为新图片文件的步骤。代码展示了从读取文件到编码和解码的完整流程,适合用于需要轻量级数据传输或存储的场景。

转载地址:http://gsofk.baihongyu.com/

你可能感兴趣的文章
python __new__中单例的作用
查看>>
python | aiofiles,一个超酷的 Python 库!
查看>>
python | akshare,一个超强的 开源Python 金融数据接口库!
查看>>
python | alabaster,一个强大的 关于alabaster 主题 Python 库!
查看>>
python | algorithms,一个超赞的 集合常用算法的Python 库!
查看>>
python调用jpype 报错:OSError JVM is already started和JVM cannot be restarted
查看>>
python | authlib,一个强大的 Python 库!
查看>>
python | awswrangler,一个高效的 Python 库!
查看>>
python | bashplotlib,一个有趣的Python库!
查看>>
python | bentoml,一个超级厉害的 模型部署 Python 库!
查看>>
python调用jar包的模块_python调用jar包
查看>>
python | black,一个神奇的 代码格式化工具 Python 库!
查看>>
python | bleach,一个超强的 Python 库!
查看>>
python | cartopy,一个有趣的 Python 库!
查看>>
python | cloud-init,一个实用的 云计算 Python 库!
查看>>
python | code2flow,一个神奇的 Python 库!
查看>>
python | cudf,一个超实用的 Python 库!
查看>>
python | daphne,一个非常nice的 Python 库!
查看>>
python调用halcon
查看>>
python | doit,一个非常实用的 Python 库!
查看>>