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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Robert Kern
John Hunter wrote:

>>>>>>"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.

StringIO isn't the issue here; being able to write to sys.stdout, which
ought to have a valid FILE* underneath, is the issue.

Why not use PIL where it's available? backend_agg2.py has a start at
this. Here's a slightly more fleshed-out (but untested) implementation
for backend_agg.py :

  if not is_string_like(filename):
      try:
          import Image
          have_pil = True
      except ImportError:
          have_pil = False
      if have_pil:
          img = Image.frombuffer('RGBA', (self.width, self.height),
                                 self.buffer_rgba(0, 0))
          img.write(filename)
      else:
          self.renderer._renderer.write_png(filename)


--
Robert Kern
[hidden email]

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter


-------------------------------------------------------
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-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Nicholas Young
On Tue, 2005-08-30 at 21:16 -0700, Robert Kern wrote:

> Why not use PIL where it's available? backend_agg2.py has a start at
> this. Here's a slightly more fleshed-out (but untested) implementation
> for backend_agg.py :
>
>   if not is_string_like(filename):
>       try:
>           import Image
>           have_pil = True
>       except ImportError:
>           have_pil = False
>       if have_pil:
>           img = Image.frombuffer('RGBA', (self.width, self.height),
>                                  self.buffer_rgba(0, 0))
>           img.write(filename)
>       else:
>           self.renderer._renderer.write_png(filename)

I've previously had a problem with the origin when doing something
similar with buffer_rgba (resulting in a vertical flip).  Doing:

            im = Image.frombuffer('RGBA', (self.width, self.height),
                                  self.buffer_rgba(0, 0),
                                  'raw', 'RGBA', 0, 1)

fixes that problem.

If a method utilising PIL images in this manner is added I'd suggest
adding a to_pil (or similar) method for those who want to process the
resulting image in some manner requiring PIL.

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-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

John Hunter-8
In reply to this post by Robert Kern
>>>>> "Robert" == Robert Kern <[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.

    Robert> StringIO isn't the issue here; being able to write to
    Robert> sys.stdout, which ought to have a valid FILE* underneath,
    Robert> is the issue.

Thanks for reminding me about this.  I thought it was possible to do
this, but had managed to forget

    import sys
    from pylab import plot, savefig, show
    plot([1,2,3])
    savefig(sys.stdout)
    show()

which I run with

  > python test.py -dAgg > test.png

produces the expected figure.

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-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel
Loading...