Reputation: 13
I'm trying to create a simple Django Web page for the following requirement:
I'm wondering how to implement this without generating temporary pictures in server.
I checked some questions related to django and matplotlib, but only find examples for either returning one picture stream by writing to Response of a picture ULR, or writing to a temp image file and then embedding to template.
RESULT:
def get_img_url():
imgbuf = cStringIO.StringIO()
pylab.savefig(imgbuf,format='png',bbox_inches='tight')
imgbuf.seek(0)
return "data:image/jpg;base64,%s" % imgbuf.getvalue().encode("base64").strip()
Upvotes: 1
Views: 488
Reputation: 1651
You could generate the image from matplotlib, base64 encode the image data, then write the data to the page via Data URI.
This way you never actually save the images to the server or database, and using Base64 you don't have to deal with binary data.
Upvotes: 2