Convert Base64 To Image In Python
Last updated January 31, 2024 by Jarvis Silva
In this tutorial I will show you how to convert base64 to image file using python, To convert base64 to image we will use the python library Base64 it allows us to decode base64 code, It comes preinstalled with python so you don’t have to install it.
Python Code To Convert Base64 To Image
# Import base64 module
import base64
# Get the base64 file data
with open('base64.txt', 'rb') as bs64_file:
bs64_data = bs64_file.read()
# Decode base64
decoded_img_data = base64.b64decode((bs64_data))
# Create image file from base64
with open('output.jpeg', 'wb') as img_file:
img_file.write(decoded_img_data)
Above is the code for converting base64 to image in python, we use the base64 decode method to decode the base64 string then we write the decoded string to a image file.
To run this program you need to have python installed, if you don’t have, then read this: Install and setup python or you can use this online python compiler.
After running the program you will see that it create a image file in your folder and if you open it you will see the image, want to know how to convert image to base64 then read this article: Convert image to base64 in python.
Here are some more python programs you will find helpful:
- Convert Base64 To Pdf In Python.
- Convert Pdf to Base64 In Python.
- Convert GPX to CSV file in python.
- Convert IPYNB file to python file.
- Convert Wav to mp3 format in python.
- Convert Miles to feet in python.
- Convert Hex To RGB In Python.
- Convert RGB To Hex In Python.
- Convert HTML To Docx In Python.
I hope you found what you were looking for from this python tutorial, and if you want more python tutorials like this, do join our Telegram channel to get updated.
Thanks for reading, have a nice day 🙂