I am trying to convert base64 image data into image file and to save it.
base64_image_str = request.POST.get('base64_image_str')
# it is smthg like: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDA......."
with open("newimage.png", "wb") as f:
f.write(base64_image_str.decode('base64'))
f.close()
also tried:
f = open("newimage.png", "wb")
f.write(decodestring(base64_image_str))
f.close()
Image is being saved but it is corrupt and cannot open it. What am I doing wrong?
The start of the string, up to the first comma, is information added by POSTing the data, and as such is not part of the base64 encoding of your file. So remove it before decoding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With