How to Encode and Decode Image to Base64 Strings in Python

Why is it the need of converting an Image to Base64 ?

Before going through Image to Base64, let’s dive a little bit over file representation. As a common knowledge, we know that every file is a binary file. In addition to this, we can say that every file is just a stream of arbitrary bytes, the values of which comes in the range between 0-255. By saying this, an image is a binary file. Let’s go through Base64 strings. In a nutshell, Base64 is a textual representation of binary data. You can call it as binary-to-text encoding scheme as well. You can find more info on Base64 strings and Binary data in the next section.

Developers often requires to transmit data over text-based protocols. For example, they have to send data as an email or store in textual formats like XML or JSON. The need of the conversion of an image to base64 is such an instance, in which developers need to transmit an image along with other textual information in JSON or XML format.

Base64 vs. Binary Data:

At this point, from things explained above, you have sufficient knowledge how every file is represented. Let’s know more about binary and base64 representation.

As we all know, binary data represents information in a format that is made of 0s and 1s. This format is known as a pure binary format. In addition to this, it is not human-readable. Also, Computers and systems that deal directly with binary data 0s and 1s use binary representation. The main advantage of storing data in binary form is that it provides efficiency in storage and processing. Briefly, it is used to store any data in its raw form like storing images, video, audio, and any data. However, it causes issues where system expects text-based storage.

image to base64. It is a base64 index table.

On the other hand, Base64 representation includes a set of 64 characters. These characters include letters (uppercase and lowercase), numbers, and to additional symbols, ‘=’ and ‘/’. Using these characters, in Base64 encoding, every three bytes of data are converted into four characters. Eventually, this process allows binary data to be represented in a text format. As a result, this makes it easily transmittable.

Encoding of an Image to Base64 in Python:

I would say, it is enough for theory. It is time to do the conversion and get the feeling of the use-case by doing some programming.

# import required library
import base64

# declare the path of the file
image_path = 'sample_image.png'

# Read the file at given location and encode it to base64 strings
with open(image_path, 'rb') as file:
     binary_data = file.read()
     base64_data = base64.b64encode(binary_data).decode()
print(base64_data)

Decoding of an Image to Base64 in Python:

# import required library
import base64

# decode base64 data
binary_data = base64.b64decode(base64_data).decode()

# declare the location of file and write the file to given location
image_path = 'sample_image.png'
with open(image_path, 'wb') as file:
     binary_data = file.write()

Leave a Reply

Top