Home > Software design >  how to convert json text to png in python
how to convert json text to png in python

Time:10-25

I have files uploaded to a server via ajax calls. The files contain images. I need to read the uploaded files and convert them to .png in python. How can I achieve that?

The images, stored as .txt files, look like this: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAm4 ... RK5CYII="

Thanks! FP

CodePudding user response:

The string starting from iV... is your actual bytes data of png converted in base 64. decode the string and with as binary to file, you will get your image

import base64
png_str = "iVBORw0 ... NS=="
png_bytes = base64.b64decode(png_str) # get your raw bytes

with open('test.png', 'bw') as fid: # write as binary file
    fid.write(png_bytes)

if it helps , please vote up.

  • Related