Python - convert image to JSON

Python 2

As the base64.encodebytes() has been deprecated in base64, the code snippet above can be modified as follows:

import json
import base64

data = {}
with open('some.gif', mode='rb') as file:
    img = file.read()

data['img'] = base64.b64encode(img)
print(json.dumps(data))

Then, use base64.b64decode(data['img']) to convert back.


This might get you started:

import json
import base64

data = {}
with open('some.gif', mode='rb') as file:
    img = file.read()
data['img'] = base64.encodebytes(img).decode('utf-8')

print(json.dumps(data))

Tags:

Python

Json