How to convert a json file to a bitmap image?

39 views Asked by At

I am having a json file "data.json" and few data to show is:

[{
    "id":"Tl134567",
    "type": "Action",
    "cmd":"something"

},
{
    "id":"Sg1235",
    "type": "Passive",
    "cmd":"something1"
}]

now I am trying to pass this file as input and trying to convert this into a bitmap image m getting a string as an output but while decoding it back m not getting the content don't know whether i have done the conversion of Json into bitmap image properly or not.

here is my code:

        file = open('data.json')
        content=json.loads(file.read())
    
        def base64_2_mask(s):
            z = zlib.decompress(base64.b64decode(s))
            n = np.fromstring(z,np.uint8 )
            mask = cv2.imdecode(n, cv2.IMREAD_UNCHANGED)[:, :, 3].astype(bool)
            return mask
    
        def mask_2_base64(mask):
            img_pil = Image.frombytes("L",(3,2),np.array(mask))
            img_pil.putpalette([0,0,0,255,255,255])
            bytes_io = io.BytesIO()
            img_pil.save(bytes_io, format='PNG', transparency=0, optimize=0)
            bytes = bytes_io.getvalue()
            return base64.b64encode(zlib.compress(bytes)).decode('utf-8')
    
        for lst in content:
            for con in lst.items():
               encoded_string = mask_2_base64(con)
       print(encoded_string)
       print("last",base64_2_mask(encoded_string))
0

There are 0 answers