Rendering a plot into a variable?

I am using pylab in a web application server (TurboGears/CherryPy).

I need to be able to render a plot (actually a pie) in png, jpg, or gif format directly into a variable that I can return to the application server. I'm currently using the rather ugly method of writing to a temp file and then reading it back in and returning the data. Is there a way to do this completely in memory?

Thanks,
Steve Bergman

Attached is a script which uses the Agg backend and the Python Imaging Library to generate the PNG as a string. The same caveats about pylab and multithreading still apply. I hope this helps you get started.

Ken

agg2png.py (1.19 KB)

···

On Mar 25, 2006, at 10:15 AM, Steve Bergman wrote:

I need to be able to render a plot (actually a pie) in png, jpg, or gif format directly into a variable that I can return to the application server. I'm currently using the rather ugly method of writing to a temp file and then reading it back in and returning the data. Is there a way to do this completely in memory?

Ken McIvor wrote:

I need to be able to render a plot (actually a pie) in png, jpg, or gif format directly into a variable that I can return to the application server. I'm currently using the rather ugly method of writing to a temp file and then reading it back in and returning the data. Is there a way to do this completely in memory?

Thanks for the response. :slight_smile:

Yes, shortly after I sumitted my question to the list I found that on the wiki. After thinking about it a bit, I decided that the creation of a temporary file, which only exists for a fraction of a second before being deleted (and therefore never makes it to disk), does not cost me that much, and is really the simpler solution. It would be nice if I could do:

image = render(figure, format='png')
return image

But hey, I'm not complaining. Matplotlib was there when I needed it and does what I need it to do.

I've been using OSS for about ten years now. And for the most part I take it for granted that I'll be able to do what I need to do with OSS.

But when you think about it, it's really pretty amazing that there is always a safety net there that works, and works well. Sometimes the safety net is aestheticly pleasing. Sometimes not. And sometimes it doesn't seem so at first, but seems more pleasing the closer you get to it.

One thing that people forget to do is thank the developers. Oh, we l^Husers do it. But we tend to note the times that we do and forget the times we don't.

Thank You All.

Oh, and thread_object.acquire(True) and thread_object.release() are still my friends. :slight_smile:

-Steve

···

On Mar 25, 2006, at 10:15 AM, Steve Bergman wrote: