|
From: Sascha <sas...@gm...> - 2005-08-30 20:04:50
|
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 |
|
From: John H. <jdh...@ac...> - 2005-08-31 03:45:36
|
>>>>> "Sascha" == Sascha <sas...@gm...> 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
|
|
From: Sascha G. <Sas...@gm...> - 2005-08-31 06:15:53
|
> 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 +++ |
|
From: Nicholas Y. <su...@su...> - 2005-08-31 10:35:40
|
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
|
|
From: Sascha <sas...@gm...> - 2005-09-02 21:54:57
|
> 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 |
|
From: John H. <jdh...@ac...> - 2005-09-02 22:02:43
|
>>>>> "Sascha" == Sascha <sas...@gm...> 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
|