Convert base64 string to image and save
I am trying to convert base64 image data to image file and 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()
The image is saved, but it is damaged and cannot open it. What am I doing wrong?
+3
source to share