Convert base64 string to Image in Python

Have you ever wondered how Images are being stored and transferred without being corrupted? Sometimes, when we open the images in their raw format, we observe that they are encoded in strange characters. Such characters represent Base64 string data. There is a need to convert them back to their original format. In this tutorial, we will learn how to convert Base64 string to Image in Python.

What is the Base64 module in Python?

Base64 is a module in python that is used for encoding and decoding data. A Base64 encoded data is the one wherein the binary form of data is represented in printable ASCII string format by translating to radix-64 representation. Decoding the data is exactly the opposite of encoding. Here the data in ASCII format is converted back to the binary data. This binary data is converted into byte-sized chunks which are converted back to the original format

Why Base64 encoding and decoding is required?

There are multiple reasons for converting the Base64 string to Image and vice versa. The following points explain the need for encoding and decoding images.

  1. Base64 is used to convert images into data that can be embedded within various formats such as HTML, CSS, JSON, etc. For instance, as the image data is already embedded in the document, the browser doesn’t need to make an additional web request to fetch the file. If we want to retrieve the images back from the embedded data, we can use base64 decoding.
  2. Base64 can also be used to encode the images such that they can be stored and transferred without being corrupted. Once the images have reached their destination, they can be decoded back to their original format.

Code to convert Base64 string to Image in Python

#importing base64 module
import base64
 
#open file with base64 string data
file = open('file1.txt', 'rb')
encoded_data = file.read()
file.close()

#decode base64 string data
decoded_data=base64.b64decode((encoded_data))

#write the decoded data back to original format in  file
img_file = open('image.jpeg', 'wb')
img_file.write(decoded_data)
img_file.close()

Input:

Base64 string data stored in file1.txt:

Convert base64 string to Image in Python

Output:

 

Output image generated after decoding the Base64 string:

Convert base64 string to Image in Python

 

How does the code to convert Base64 string to Image work?

The following steps give the working of the above code to convert the base64 string to Image in Python:

  1. First, we import the base64 module
  2. Then open the file which contains base64 string data for an image. We do this using the open() function in python. The open() function takes two parameters-the file to be opened and the mode. In our case, the mode is ‘rb’ (read binary).
  3. We take the binary data and store it in a variable. Then we close the file.
  4. We decode the Base64 string data using the b64decode() function of the base64 module. This function takes the encoded data as a parameter.
  5. We create a file named image.jpeg to store the decoded data in its original Image format. To write the data onto the file, we use the write() function in Python. The function uses the mode as ‘wb’ (write binary). We then finally close the file.

Thus we have reached the end of the tutorial.
You can learn how to convert Image to Base64 string data from the following link: Base64 to Image

Leave a Reply

Your email address will not be published.