如何解码 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学习者快速成长!