@@ -322,9 +322,13 @@ def print_figure(self, filename, dpi=150,
322322
323323 If the extension matches BMP or RAW, write an RGBA bitmap file
324324
325+ If filename is a fileobject, write png to file object (thus
326+ you can, for example, write the png to stdout
325327 """
326328 if DEBUG : print 'FigureCanvasAgg.print_figure'
327329
330+
331+
328332 # store the orig figure dpi, color and size information so we
329333 # can restore them later. For image creation alone, this is
330334 # not important since after the print the figure is done. But
@@ -344,34 +348,38 @@ def print_figure(self, filename, dpi=150,
344348 # render the printed figure
345349 self .draw ()
346350
347- # take a look at the extension and choose the print handler
348- basename , ext = os .path .splitext (filename )
349- if not len (ext ):
350- ext = '.png'
351- filename += ext
352-
353- ext = ext .lower ()
354- if (ext .find ('rgb' )>= 0 or
355- ext .find ('raw' )>= 0 or
356- ext .find ('bmp' )>= 0 ):
357- # agg doesn't handle unicode yet
358- self .renderer ._renderer .write_rgba (str (filename ))
359- elif ext .find ('png' )>= 0 :
360- # agg doesn't handle unicode yet
361- self .renderer ._renderer .write_png (str (filename ))
362- #pass
363- elif ext .find ('svg' )>= 0 :
364- from backend_svg import FigureCanvasSVG
365- svg = self .switch_backends (FigureCanvasSVG )
366- svg .figure .dpi .set (72 )
367- svg .print_figure (filename , 72 , facecolor , edgecolor , orientation )
368- elif ext .find ('ps' )>= 0 or ext .find ('ep' )>= 0 :
369- from backend_ps import FigureCanvasPS # lazy import
370- ps = self .switch_backends (FigureCanvasPS )
371- ps .figure .dpi .set (72 )
372- ps .print_figure (filename , 72 , facecolor , edgecolor , orientation )
351+ if isinstance (filename , file ):
352+ # assume png and write to fileobject
353+ self .renderer ._renderer .write_png (filename )
373354 else :
374- error_msg ('Do not know know to handle extension *%s' % ext )
355+ # take a look at the extension and choose the print handler
356+ basename , ext = os .path .splitext (filename )
357+ if not len (ext ):
358+ ext = '.png'
359+ filename += ext
360+
361+ ext = ext .lower ()
362+ if (ext .find ('rgb' )>= 0 or
363+ ext .find ('raw' )>= 0 or
364+ ext .find ('bmp' )>= 0 ):
365+ # agg doesn't handle unicode yet
366+ self .renderer ._renderer .write_rgba (str (filename ))
367+ elif ext .find ('png' )>= 0 :
368+ # agg doesn't handle unicode yet
369+ self .renderer ._renderer .write_png (str (filename ))
370+ #pass
371+ elif ext .find ('svg' )>= 0 :
372+ from backend_svg import FigureCanvasSVG
373+ svg = self .switch_backends (FigureCanvasSVG )
374+ svg .figure .dpi .set (72 )
375+ svg .print_figure (filename , 72 , facecolor , edgecolor , orientation )
376+ elif ext .find ('ps' )>= 0 or ext .find ('ep' )>= 0 :
377+ from backend_ps import FigureCanvasPS # lazy import
378+ ps = self .switch_backends (FigureCanvasPS )
379+ ps .figure .dpi .set (72 )
380+ ps .print_figure (filename , 72 , facecolor , edgecolor , orientation )
381+ else :
382+ error_msg ('Do not know know to handle extension *%s' % ext )
375383
376384 # restore the original figure properties
377385 self .figure .dpi .set (origDPI )
0 commit comments