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.

2 Answers 2

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"))) 
Sign up to request clarification or add additional context in comments.

1 Comment

this doesnt include anywhere how should i inject the base64 byte in the image
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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.