首页  >  文章  >  后端开发  >  如何解码 Base64 编码的图像并将其保存到磁盘?

如何解码 Base64 编码的图像并将其保存到磁盘?

Barbara Streisand
Barbara Streisand原创
2024-10-20 07:48:29298浏览

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn