转化与解析流程:二进制读取---转为base64(b64encode)--- 解码(b64decode)--- 输出图片/语音文件
# 实现base64的转化与解析
with open('test.png', 'rb') as f: # 二进制的方式打开图片
byte_data = base64.b64encode(f.read()) # 读取文件内容,转换为base64编码,输出的为bytes类型
str = byte_data.decode('UTF-8') # 将bytes格式转为字符串
with open('./data/pic_base64.txt', 'w') as out_file: # 将转化的base64字符串输出到文件中
out_file.write(str)
# print(str)
# 将base64解码,并输出为图片
imagedata = base64.b64decode(byte_data) # 解码为二进制
print(type(imagedata))
with open('out.png', 'wb') as out_pic:
out_pic.write(imagedata)
语音文件同理按如上处理