Skip to main content
 首页 » 编程设计

python之使用 Python 将数据转换为图像

2024年09月07日8zhengyun_ustc

<分区>

在 Python 中转换非常简单,关键部分是使用“base64”模块,该模块提供标准数据编码和解码。 将图像转换为字符串

这是将图像转换为字符串的代码。

import base64 
 
with open("t.png", "rb") as imageFile: 
    str = base64.b64encode(imageFile.read()) 
    print str 

将字符串转换为图像

以下代码段将使用给定的字符串创建图像。

fh = open("imageToSave.png", "wb") 
fh.write(str.decode('base64')) 
fh.close() 

但是这里是t.png

这是 imageToSave.png

到目前为止,如果有什么地方做错了,请告诉我。 这是错误跟踪

'base64' is not a text encoding; use codecs.decode() to handle arbitrary codecs 

感谢您的宝贵时间。