Home >Backend Development >Python Tutorial >How to Convert and Save a Base64-Encoded Image to the Filesystem in Python?
Given a string in base64 format, which represents a PNG image, a common task is to need to save this image to the filesystem, as a PNG file.
Here are steps to convert a base64 string to an image and save it to the filesystem using Python:
<code class="python">import base64 img_data = base64.b64decode(base64_string)</code>
<code class="python">with open("image.png", "wb") as f:</code>
<code class="python"> f.write(img_data)</code>
<code class="python">f.close()</code>
Here is a complete example:
<code class="python">import base64 # Replace "base64_string" with the actual base64-encoded string base64_string = "" img_data = base64.b64decode(base64_string) with open("image.png", "wb") as f: f.write(img_data)</code>
This will save the PNG image to a file named "image.png" in the current directory.
The above is the detailed content of How to Convert and Save a Base64-Encoded Image to the Filesystem in Python?. For more information, please follow other related articles on the PHP Chinese website!