在 Python 中使用 Base64 对图像文件进行编码和解码
2023-08-09 本文已影响0人
原上的小木屋
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)