Home >Backend Development >Python Tutorial >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!