def _create_png(figure):
 canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(figure)
 canvas.draw()
 size = canvas.get_renderer().get_canvas_width_height()
 image_as_string = canvas.tostring_rgb()
 image = PIL.Image.fromstring('RGB', size, image_as_string, 'raw', 'RGB', 0, 1)
 image_background = PIL.Image.new(image.mode, image.size, figure.get_facecolor())
 non_whitespace = PIL.ImageChops.difference(image, image_background)
 bounding_box = non_whitespace.getbbox()
 image = image.crop(bounding_box)
 image_data = StringIO.StringIO()
 image.save(image_data, format='PNG')
 return (image_data.getvalue(), bounding_box)

def _create_png ( figure ) : canvas = matplotlib . backends . backend_agg . FigureCanvasAgg ( figure ) canvas . draw ( ) size = canvas . get_renderer ( ) . get_canvas_width_height ( ) image_as_string = canvas . tostring_rgb ( ) image = PIL . Image . fromstring ( 'RGB' , size , image_as_string , 'raw' , 'RGB' , 0 , 1 ) image_background = PIL . Image . new ( image . mode , image . size , figure . get_facecolor ( ) ) non_whitespace = PIL . ImageChops . difference ( image , image_background ) bounding_box = non_whitespace . getbbox ( ) image = image . crop ( bounding_box ) image_data = StringIO . StringIO ( ) image . save ( image_data , format = 'PNG' ) return ( image_data . getvalue ( ) , bounding_box )
given the matplotlib figure .