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.
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