"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Convert Base64 String to PNG Image and Save to File?

How to Convert Base64 String to PNG Image and Save to File?

Published on 2024-12-22
Browse:148

How to Convert Base64 String to PNG Image and Save to File?

Convert String in Base64 to Image and Save on Filesystem

Problem:
I have a string in base64 format, which represents a PNG image. Is there a way to save this image to the filesystem, as a PNG file?

Answer:

import base64

# Decode the base64 string into bytes
image_data = base64.decodebytes(base64_string)

# Write the decoded bytes to a file
with open("image.png", "wb") as f:
    f.write(image_data)

This code will create a PNG file named "image.png" in the current working directory.

Release Statement This article is reprinted at: 1729381878 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3