3333def _fn_name (): return sys ._getframe (1 ).f_code .co_name
3434
3535from matplotlib import verbose
36- from matplotlib .numerix import asarray , pi #, fromstring, UInt8, zeros, where, transpose, nonzero, indices, ones, nxfrom matplotlib._matlab_helpers import Gcf
36+ from matplotlib .numerix import asarray , pi , fromstring , UInt8 #, zeros, where,
37+ #transpose, nonzero, indices, ones, nx
38+ #from matplotlib._matlab_helpers import Gcf
3739from matplotlib .backend_bases import RendererBase , GraphicsContextBase ,\
3840 FigureManagerBase , FigureCanvasBase
3941from matplotlib .cbook import enumerate , True , False
@@ -179,9 +181,33 @@ def draw_image(self, x, y, im, origin, bbox):
179181 None
180182 """
181183 if DEBUG : print 'backend_cairo.RendererCairo.%s()' % _fn_name ()
182- pass
183- # how does cairo do this?
184-
184+ # why is there no gc arg?
185+
186+ try : import cairo .numpy
187+ except :
188+ verbose .report_error ("cairo.numpy module required for draw_image()" )
189+ return
190+
191+ ctx = cairo .Context ()
192+ ctx .set_target_surface (self .surface )
193+ ctx .set_matrix (self .matrix )
194+
195+ # bbox - not used
196+ flipud = origin == 'lower'
197+ rows , cols , s = im .as_str (flipud )
198+
199+ X = fromstring (s , UInt8 )
200+ X .shape = rows , cols , 4
201+ # ARGB32 is incompatible with Image format? - colors are wrong
202+ surface = cairo .numpy .surface_create_for_array (X )
203+
204+ # Alternative
205+ #surface = cairo.surface_create_for_image(buffer, cairo.FORMAT_ARGB32, cols, rows) #, stride)
206+ # error: TypeError: Cannot use string as modifiable buffer
207+
208+ ctx .translate (x ,y )
209+ ctx .show_surface (surface , cols , rows )
210+
185211
186212 def draw_line (self , gc , x1 , y1 , x2 , y2 ):
187213 """
@@ -485,6 +511,7 @@ def print_figure_fn(figure, filename, dpi=150, facecolor='w', edgecolor='w',
485511 override this on hardcopy
486512
487513 orientation - only currently applies to PostScript printing.
514+ filename - can also be a file object, png format is assumed
488515 """
489516 if DEBUG : print 'backend_cairo.FigureCanvasCairo.%s()' % _fn_name ()
490517
@@ -507,7 +534,10 @@ def print_figure_fn(figure, filename, dpi=150, facecolor='w', edgecolor='w',
507534 figure .set_edgecolor (edgecolor )
508535
509536 ext = ext .lower ()
510- if ext in ('png' , 'ps' ):
537+ if isinstance (filename , file ):
538+ # for fileobjects assume PNG format
539+ _save_png (figure , filename )
540+ elif ext in ('png' , 'ps' ):
511541 try :
512542 fileObject = file (filename ,'wb' )
513543 except IOError , exc :
0 commit comments