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

DDD
Release: 2024-10-20 07:55:02
Original
962 people have browsed it

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>
Copy after login

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!

source:php
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!