def get_base64_image(self, obj): f = open(obj.image_file.path, 'rb') image = File(f) data = base64.b64encode(image.read()) f.close() return data code example
Example: python convert image to base64
import base64
file = 'deer.jpg'
image = open(file, 'rb')
image_read = image.read()
image_64_encode = base64.encodebytes(image_read)
print('This is the image in base64: ' + str(image_64_encode))
image_64_decode = base64.decodebytes(image_64_encode)
image_result = open('deer_decode.jpg', 'wb')
image_result.write(image_64_decode)