在 Python 中使用 Base64 对图像文件进行编码和解码

import base64

# 将图像文件编码为 Base64
with open("image.png", "rb") as image_file:
    encoded_image = base64.b64encode(image_file.read())

with open("encoded_image.txt", "wb") as encoded_file:
    encoded_file.write(encoded_image)

# 将 Base64 编码的字符串解码为图像文件
with open("encoded_image.txt", "rb") as encoded_file:
    encoded_image = encoded_file.read()

image_data = base64.b64decode(encoded_image)

with open("decoded_image.png", "wb") as image_file:
    image_file.write(image_data)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容