如何解碼 Base64 編碼的圖像並將其儲存到磁碟?

Barbara Streisand
發布: 2024-10-20 07:48:29
原創
291 人瀏覽過

How to Decode a Base64-Encoded Image and Save it to Disk?

將base64編碼的字串轉換為映像並將其儲存到檔案系統

問題

我有一個base64格式的字串表示PNG 圖像。如何將此圖像作為 PNG 檔案保存到檔案系統?

答案

<code class="python">import base64

# Decode the base64-encoded string and convert it into binary data.
binary_data = base64.b64decode(string_data)

# Save the binary data to a file.
with open('image.png', 'wb') as f:
    f.write(binary_data)</code>
登入後複製

在此範例中,string_data 是一個 base64 編碼的字串,image.png 是所需的保存影像的檔案名稱。

以上是如何解碼 Base64 編碼的圖像並將其儲存到磁碟?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!