Home >Backend Development >Python Tutorial >How to Decode a Base64-Encoded Image and Save it to Disk?
I have a string in base64 format representing a PNG image. How do I save this image to the file system as a PNG file?
<code class="python">import base64 # Decode the base64-encoded string and convert it into binary data. binary_data = base64.b64decode(string_data) # Save the binary data to a file. with open('image.png', 'wb') as f: f.write(binary_data)</code>
In this example, string_data is a base64-encoded string, and image.png is the desired filename for the saved image.
The above is the detailed content of How to Decode a Base64-Encoded Image and Save it to Disk?. For more information, please follow other related articles on the PHP Chinese website!