簡體   English   中英

如何在python中對二進制文件進行base64編碼/解碼?

[英]How to base64 encode/decode binary files in python?

我正在嘗試使用以下簡單代碼使用 python 對同一圖像文件進行編碼和解碼。 但是每次輸出文件都比輸入文件大而且打不開。 這段代碼有什么問題?

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" 您現在正在以不同的格式進行寫作和閱讀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM