Effe Pelosa
Effe Pelosa

Reputation: 173

how to convert json text to png in python

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

Upvotes: 0

Views: 418

Answers (1)

amirhm
amirhm

Reputation: 1249

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.

Upvotes: 2

Related Questions