Python: embedding images in JSON objects with base64

Giuseppe Maggi
3 min readMar 12, 2024

--

Base64 is an encoding that allows to convert binary data in a sequence of characters. The text format offers the advantage to be transferable in many ways and it’s important when we talk about network communication.

If we had to send an image with other information (e.g. metadata, a timestamp, the name, the place where the picture was taken), we could incorporate it in a JSON object encoded in base64.

In this post, we’re going to cover this topic in Python language exploring the base64 encoding, embedding an image in a JSON object and then extracting it.

The encoding

What base64 encoded file looks like? It’s an alphanumeric sequence but here we don’t go deeper in further details about that. We’re going to restrict ourselves to only look at the appearance of a base64 string.

For example, the sentence “Hi everybody” encoded in base64 is SGkgZXZlcnlib2R5Cg==.

You can try this convertion with the Linux command base64:

$ echo "Hi everybody" > encoded.txt
$ base64 encoded.txt
SGkgZXZlcnlib2R5Cg==

and, for confirmation, you can do the inverse operation:

$ echo 'SGkgZXZlcnlib2R5Cg==' | base64 -d
Hi everybody

The same example in Python language looks like this:

import base64

sentence = "Hi everybody"
bytes_format = sentence.encode('ascii')
base64_bytes = base64.b64encode(bytes_format)
base64_sequence = base64_bytes.decode('ascii')

The variable base64_sequence contains the encoded sequence. The process can be inverted like this:

base64_bytes = base64_sequence.encode('ascii')
bytes_format = base64.b64decode(base64_bytes)
sentence = bytes_format.decode('ascii')

Creating a JSON file with the embedded image

It’s time to proceed with our real example creating a JSON document including the image, encoded in base64. Let’s suppose that we want to define a JSON structure in which we can include more images. In the document, we have a list corresponding to the images property. For each image, we have a JSON document with several attributes such as timestamp, author, collection_name and content, this one containing the base64 encoded image.

To run the code, we need the Pillow library that can be installed with:

$ pip install pillow

This isn’t the only way to encode an image in base64 but it’s the one we chose.

Here is the example:

from io import BytesIO
import base64, json
from PIL import Image

filename='picture.jpg'
with open(filename, 'rb') as imgfile:
base64_bytes = base64.b64encode(imgfile.read())
base64_encoded = base64_bytes.decode()
data={
'images':[
{
'name':filename,
'content': base64_encoded,
'timestamp': 1545149708,
'author': 'jack'
}
],
'collection_name': 'best photos of the year'
}

with open('embedded.json', 'w') as f:
json.dump(data, f)

At the end, we saved the JSON document in a file using the dump function of Python json libray. Opening the embedded.json file isn’t recommended given its size, anyway its content will look like this:

{"images": 
[{"name": "picture.jpg",
"content": "/9j/4AAQSkZJRg...SJQnjtWVf//Z",
"timestamp": 1545149708,
"author": "jack"}
],
"collection_name": "best photos of the year"
}

As shown, we shortened the encoded string in the content property to make the representation more concise.

Extracting the image from the JSON file

The following code shows how to extract the image we included in the JSON file. Obviously, the code depends on the structure we created for the object. In fact, we get the value of content of the first element in the images list:

from io import BytesIO
import base64, json
from PIL import Image

with open('embedded.json', 'r') as f:
data_received=json.load(f)

im = Image.open(BytesIO(base64.b64decode(data_received['images'][0]['content'])))
im.save('picture_out.jpg', 'JPEG')

The file picture_out.jpg should contain the same image we had in the beginning of the example although with a different size due to the convertions it underwent.

--

--

Giuseppe Maggi

Software Engineer | AWS Solutions Architect | Technical writer. I write about AWS, Cloud in general, Blockchain and Data Science