rasterizing a figure

Hi, What is the best way to rasterize a figure into a numpy array for further image processing? The code I have below works, but I am wondering if there is a more convenient way.

Thanks in advance, Geoff

import pylab, numpy
fig = pylab.figure()
pylab.plot( [1, 2, 1] )
dpi = fig.get_dpi()
size = fig.get_size_inches()
shape = size[1] * dpi, size[0] * dpi, 3
image = fig.canvas.renderer.tostring_rgb()
image = numpy.fromstring( image, 'u1' ).reshape( shape )
pylab.imshow( image )

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.Barker@...259...

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

···

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 )

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

···

On Thu, Nov 12, 2009 at 6:32 PM, Geoffrey Ely <gely@...1887...> wrote:

Thanks for the help. With that I get:

AttributeError: 'function' object has no attribute '__buffer__'

Work perfectly. Thanks, JJ!

-Geoff

···

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)