Re: [Matplotlib-users] rasterizing a figure
On Nov 12, 2009, at 4:32 PM, Jae-Joon Lee wrote: > shape = fig.canvas.get_width_height()[::-1] + (4,) > > im = np.frombuffer(fig.canvas.buffer_rgba(0,0), > dtype=np.uint8).reshape(shape) > > Using buffer_rgba is more efficient than using tostring_rgb as no > memory is allocated. But, I believe that "im" is a read-only "view" of > the canvas. So, you must make a copy if you want to show it with > imshow as in your original code. > > If you're using PIL, > > import PIL.Image > > im2=PIL.Image.frombuffer("RGBA", fig.canvas.get_width_height(), > fig.canvas.buffer_rgba(0,0), "raw", "RGBA", > 0, 1) Work perfectly. Thanks, JJ! -Geoff -- Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] rasterizing a figure
On Thu, Nov 12, 2009 at 6:32 PM, Geoffrey Ely wrote: > Thanks for the help. With that I get: > > AttributeError: 'function' object has no attribute '__buffer__' > > shape = fig.canvas.get_width_height()[::-1] + (4,) im = np.frombuffer(fig.canvas.buffer_rgba(0,0), dtype=np.uint8).reshape(shape) Using buffer_rgba is more efficient than using tostring_rgb as no memory is allocated. But, I believe that "im" is a read-only "view" of the canvas. So, you must make a copy if you want to show it with imshow as in your original code. If you're using PIL, import PIL.Image im2=PIL.Image.frombuffer("RGBA", fig.canvas.get_width_height(), fig.canvas.buffer_rgba(0,0), "raw", "RGBA", 0, 1) Regards, -JJ -- Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] rasterizing a figure
On Nov 12, 2009, at 2:01 PM, Christopher Barker wrote: > Geoffrey Ely wrote: >> Hi, What is the best way to rasterize a figure into a numpy array for >> further image processing? > > untested, but I suspect: >> image = >> numpy.frombuffer( fig.canvas.renderer.buffer_rgba).reshape( shape ) Thanks for the help. With that I get: AttributeError: 'function' object has no attribute '__buffer__' I found a cleaner way to get the shape, so this is getting better: shape = fig.canvas.get_width_height()[::-1] + (3,) image = numpy.fromstring( fig.canvas.renderer.tostring_rgb(), 'u1' ).reshape( shape ) -Geoff -- Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] rasterizing a figure
Geoffrey Ely wrote: > Hi, What is the best way to rasterize a figure into a numpy array for > further image processing? untested, but I suspect: > image = numpy.frombuffer( fig.canvas.renderer.buffer_rgba).reshape( shape ) might work -- something like that, anyway. -CHB -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R(206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception chris.bar...@noaa.gov -- Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users