SourceForge logo
SourceForge logo
Menu

matplotlib-users

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
> 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
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.
Thanks for helping keep SourceForge clean.
X





Briefly describe the problem (required):
Upload screenshot of ad (required):
Select a file, or drag & drop file here.
Screenshot instructions:

Click URL instructions:
Right-click on the ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)

More information about our ad policies

Ad destination/click URL:

AltStyle によって変換されたページ (->オリジナル) /