Render image to string

I am using matplotlib in a django application and would like to render a
graph and return it directly.

Currently I have

    plt.savefig("some/location.png")

working. But since the images I will be generating are only EVER needed
once, I would like to simply return the image directly through django
without needing to save it, then return an URI.

so instead of doing

    plt.savefig("some/location.png")

    return HttpResponse("<html><body><img src='some/location.png'
/></body></html>")

I could do

    return HttpResponse(plt.image_render('image/png'), mimetype="image/png")

and not have to worry about deleting the image later (there are LOT that
get generated)

Any help would be greatly appreciated.
~Doug

You can save directly to a file handle (eg sys.stdout) so you need not
go to a PNG file. Alternatively, you can write to a StringIO object.
See the examples at

  http://matplotlib.sourceforge.net/faq/howto_faq.html#matplotlib-in-a-web-application-server

And if you would be willing to complete the FAQ stub for Django

  http://matplotlib.sourceforge.net/faq/howto_faq.html#matplotlib-with-django

That would be much appreciated. See

  http://matplotlib.sourceforge.net/faq/howto_faq.html#how-to-contribute-docs

Thanks,
JDH

···

On Tue, Jul 7, 2009 at 6:41 PM, Doug Penner<darwinsurvivor@...287...> wrote:

so instead of doing

plt.savefig("some/location.png")

return HttpResponse("<html><body><img src='some/location.png'
/></body></html>")

I could do

return HttpResponse(plt.image_render('image/png'), mimetype="image/png")

and not have to worry about deleting the image later (there are LOT that
get generated)