如何在 Python 中将 Base64 编码的 PNG 字符串转换为图像并将其保存到文件系统?

DDD
发布: 2024-10-20 07:55:02
原创
962 人浏览过

How to Convert Base64 Encoded PNG Strings to Images and Save Them to the Filesystem in Python?

将 Base64 格式的字符串转换为图像并保存在文件系统上

如果您有一个代表 PNG 图像的 Base64 格式的字符串,这里是如何将其转换为图像并将其保存到 Python 中的文件系统:

<code class="python">import base64
import io

# Decode the base64 string into binary data
binary_data = base64.b64decode(base64_string)

# Create a file-like object from the binary data
image_file = io.BytesIO(binary_data)

# Save the image to a file
with open('image_name.png', 'wb') as f:
    f.write(image_file.read())</code>
登录后复制

以上是如何在 Python 中将 Base64 编码的 PNG 字符串转换为图像并将其保存到文件系统?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!