首页  >  文章  >  后端开发  >  如何在 Python 中将 Base64 编码的 PNG 字符串转换为图像并将其保存到文件系统?

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

DDD
DDD原创
2024-10-20 07:55:02967浏览

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

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