Skip to content Skip to sidebar Skip to footer

How To Convert Image Data So It Can Be Json Serializable And Vice Versa

How can i make image data JSON serializable and then convert it back so it can be saved as a image. I get the error: is not

Solution 1:

img_file = request.session.get('image_file')
json.dumps(str(my_imagefield)) 

this should work for storing the image in the memory

or using base64

import base64
img_file = request.session.get('image_file')
with open(img_file , "wb") as fh:
    fh.write(base64.decodebytes(img_data))

Solution 2:

You have a few options and which you use will vary on your use case.

You can get the raw bytes of the image and return the serialized byte array.

You can get the raw bytes of the image and convert the bytes to hex and return the hex string.

Or you can get the raw bytes of the image and convert the bytes to a base64 string and return the base64 string.

As a side note InMemoryUploadedFile is a wrapper to to get the actual file you need to use .file

Post a Comment for "How To Convert Image Data So It Can Be Json Serializable And Vice Versa"