如何在python中对64位二进制文​​件进行base64编码/解码? [英] How to base64 encode/decode binary files in python?

查看:65
本文介绍了如何在python中对64位二进制文​​件进行base64编码/解码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下简单代码使用python对相同的图像文件进行编码和解码.但是每次输出文件都大于输入文件,并且无法打开.这段代码有什么问题?

I'm trying to encode and decode the same image file using python using following simple code. But every time output file is larger than input file and it can't open. What's the problem in this code?

import base64

with open("img.jpeg", "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())

    decoded_string = base64.b64decode(encoded_string)
    with open("test_img.jpeg", "w") as image_file2:
        image_file2.write(decoded_string);

原始文件: https://filebin.ca/3j6aIDlWEYdV/img.jpeg
结果文件: https://filebin.ca/3j6arBo85Lcg/test_img.jpeg

推荐答案

尝试将写入模式更改为"wb" .您现在的写作和阅读都采用了不同的格式.

Try changing the write mode to "wb". Your writing and reading as different formats right now.

这篇关于如何在python中对64位二进制文​​件进行base64编码/解码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆