Home  >  Article  >  Backend Development  >  How to Convert Base64 Encoded PNG Strings to Images and Save Them to the Filesystem in Python?

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

DDD
DDDOriginal
2024-10-20 07:55:02967browse

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

Convert string in base64 to image and save on filesystem

If you have a string in base64 format, which represents a PNG image, here's how you can convert it to an image and save it to the filesystem in 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>

The above is the detailed content of How to Convert Base64 Encoded PNG Strings to Images and Save Them to the Filesystem in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn