Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string formatted as base64 to base64 object

From a post request, I receive a base64 in JSON. Problem is that I can't just change the file type from string to base64 since it's already formatted as base64. need the base64 to convert it back to an image.

json_data = request.get_json(force=True)
img = json_data['img']
print(img)
with open("imageToSave.png", "wb") as fh:
   fh.write(base64.decodebytes(img))
like image 281
Achiel Volckaert Avatar asked Feb 24 '26 09:02

Achiel Volckaert


1 Answers

In order to decode it, add the third line which decodes the string to base64

json_data = request.get_json(force=True)
img = json_data['img']
imgdata = base64.b64decode(img)
filename = 'upload/newimg.jpg'  # I assume you have a way of picking unique filenames
    with open(filename, 'wb') as f:
        f.write(imgdata)
like image 187
Achiel Volckaert Avatar answered Feb 25 '26 22:02

Achiel Volckaert