Saving a figure to StringIO or similar

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Saving a figure to StringIO or similar

Sascha-4
I am writing a web server app that creates charts among other things. I am
trying to get rid of the temporary file that I use to transmit the figures
created with matplotlib to the actual web server. Although print_figure says
"If filename is a fileobject, write png to file object (thus you can, for
example, write the png to stdout)" I can't successfully write anything to
stdout. Anyone knows an example or can give me some hint what I can do to
get rid of the tempfile?

Thanks,
Sascha



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Saving a figure to StringIO or similar

John Hunter-8
>>>>> "Sascha" == Sascha  <[hidden email]> writes:

    Sascha> I am writing a web server app that creates charts among
    Sascha> other things. I am trying to get rid of the temporary file
    Sascha> that I use to transmit the figures created with matplotlib
    Sascha> to the actual web server. Although print_figure says "If
    Sascha> filename is a fileobject, write png to file object (thus
    Sascha> you can, for example, write the png to stdout)" I can't
    Sascha> successfully write anything to stdout. Anyone knows an
    Sascha> example or can give me some hint what I can do to get rid
    Sascha> of the tempfile?

Short answer: no known way to do this currently, though we'd like to
figure it out.  As far as I know (and could very well be wrong)
libpng requires a FILE*, which StringIO and cStringIO do not provide.

JDH


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: [Matplotlib-users] Saving a figure to StringIO or similar

Sascha-4
> As far as I know (and could very well be wrong)
> libpng requires a FILE*, which StringIO and cStringIO do not provide.

I think this is exactly the reason why printing to stdout fails.

Well, if anybody has any other idea I'd be very grateful.

Sascha

--
5 GB Mailbox, 50 FreeSMS http://www.gmx.net/de/go/promail
+++ GMX - die erste Adresse f?r Mail, Message, More +++


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Re: [Matplotlib-users] Saving a figure to StringIO or similar

Nicholas Young
On Wed, 2005-08-31 at 08:15 +0200, Sascha GL wrote:
> > As far as I know (and could very well be wrong)
> > libpng requires a FILE*, which StringIO and cStringIO do not provide.
>
> I think this is exactly the reason why printing to stdout fails.
>
> Well, if anybody has any other idea I'd be very grateful.

I posted this to the list a few days ago:

Using the agg backend you can obtain an RGBA buffer or RGB string which
can then be loaded as a PIL Image for processing.  I've adapted a the
examples/agg_oo.py to demonstrate.

----
from matplotlib.backends.backend_agg \
    import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import Image

fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.plot([1,2,3])
ax.set_title('hi mom')
ax.grid(True)
ax.set_xlabel('time')
ax.set_ylabel('volts')
canvas.draw()
size = canvas.get_width_height()
usebuffer = True
if usebuffer:
    # Load the agg buffer directly as the source of the PIL image
    # - could be less stable as agg and PIL share memory.
    buf = canvas.buffer_rgba()
    im = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
else:
    # Save the agg buffer to a string and load this into the PIL image.
    buf = canvas.tostring_rgb()
    im = Image.fromstring('RGB', size, buf, 'raw', 'RGB', 0, 1)

im.show()
----

If you are using a recent CVS version of mpl you will need to change
buffer_rgba() to buffer_rgba(0,0).

Nick



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Re: [Matplotlib-users] Saving a figure toStringIO or similar

Sascha-4
> I posted this to the list a few days ago:
>
> Using the agg backend you can obtain an RGBA buffer or RGB string which
> can then be loaded as a PIL Image for processing.  I've adapted a the
> examples/agg_oo.py to demonstrate.

Thanks a lot, Nicolas! Excellent... no more temp file! It took me some time
to figure out how to get the PNG image data from PIL, but it worked out
quite well.

Sascha



-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Re: [Matplotlib-users] Saving a figure toStringIO or similar

John Hunter-8
>>>>> "Sascha" == Sascha  <[hidden email]> writes:

    >> Using the agg backend you can obtain an RGBA buffer or RGB
    >> string which can then be loaded as a PIL Image for processing.
    >> I've adapted a the examples/agg_oo.py to demonstrate.

    Sascha> Thanks a lot, Nicolas! Excellent... no more temp file! It
    Sascha> took me some time to figure out how to get the PNG image
    Sascha> data from PIL, but it worked out quite well.


Could you post a complete example so that others won't have to spend
that extra time figuring out how to put all the pieces together?

Thanks,
JDH


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users