1

I am facing a problem in converting base64byte to image.I tried this

filename="chart.png"
    with open(filename, "wb") as f:
       f.write(base64.decodebytes(data))

where the base64byte is sored in data (like this b'iV.....'.after this i tried uploading the "filename" to my django database by doing

gimage.objects.create(name='Rufus5', pic1=filename)

.this creates an empty file chart.png in my django database.

Vivek Anand
  • 381
  • 1
  • 10

2 Answers2

1

you can use django's default functionality

from django.core.files.images import ImageFile

gimage.objects.create(name='Rufus5', pic1=ImageFile(open("chart.png", "rb")))
Pruthvi Barot
  • 1,948
  • 3
  • 14
1

Try this:

import base64
imgdata = base64.b64decode(imgstring)
filename = 'some_image.jpg'  # I assume you have a way of picking unique filenames
with open(filename, 'wb') as f:
    f.write(imgdata)
# f gets closed when you exit the with statement
# Now save the value of filename to your database

the anser is founded in : How to convert base64 string to image?