Converting figure to numpy array

Hi,

I want to convert the current figure to a numpy array w/o saving the
image to disk. I need this to convert a matlab function to python, the
original code is:
F = getframe(gcf);
X = frame2im(F);

My current workaround is
h = gcf()
h.savefig('tmp.png')
X = imread('tmp.png')
which is obviously not viable. Also I cannot use a different backend.
TkAgg seems to be the only backend running smoothly with ipython on my
machine.
I tried several functions but I couldn't find the right one. I'm using
matplotlib 0.98.6svn and TkAgg as backend.

Someone knows how to do this neatly?

Cheers

Philipp

- --
Philipp Lies

Max Planck Institute for Biological Cybernetics
Computational Vision & Neuroscience Group
Spemannstr. 41
D-72076 Tuebingen
Germany

Phone: +49-7071-601-1788
Fax: +49-7071-601-552
E-Mail: philipp.lies@...368...
http://www.kyb.mpg.de/bethgegroup

canvas = gcf().canvas
buf = canvas.tostring_rgb()

img = numpy.fromstring(buf, numpy.uint8)
im_size = canvas.get_width_height()
rgb = img.reshape(im_size[1], im_size[0], 3)
r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]

Or if you're using PIL,

pil_image = PIL.Image.fromstring("RGB", im_size, buf)

IHTH,

-JJ

···

On Tue, Mar 24, 2009 at 10:34 AM, Philipp Lies <philipp.lies@...368...> wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I want to convert the current figure to a numpy array w/o saving the
image to disk. I need this to convert a matlab function to python, the
original code is:
F = getframe(gcf);
X = frame2im(F);

My current workaround is
h = gcf()
h.savefig('tmp.png')
X = imread('tmp.png')
which is obviously not viable. Also I cannot use a different backend.
TkAgg seems to be the only backend running smoothly with ipython on my
machine.
I tried several functions but I couldn't find the right one. I'm using
matplotlib 0.98.6svn and TkAgg as backend.

Someone knows how to do this neatly?

Cheers

Philipp

- --
Philipp Lies

Max Planck Institute for Biological Cybernetics
Computational Vision & Neuroscience Group
Spemannstr. 41
D-72076 Tuebingen
Germany

Phone: +49-7071-601-1788
Fax: +49-7071-601-552
E-Mail: philipp.lies@...368...
http://www.kyb.mpg.de/bethgegroup
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAknI71kACgkQaEa3WOxVB2w7dwCdFakgnE7RuIQniI0lJyNgUXrK
gsYAmwcdesxLdH6/bS5gQ/LVnsk7lCQm
=zhEv
-----END PGP SIGNATURE-----

------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Perhaps we should make this a friendly helper method of the agg backend canvas – canvas.toarray() returns an rgba numpy array via canvas.buffer_rgba?

···

On Tue, Mar 24, 2009 at 10:46 AM, Jae-Joon Lee <lee.j.joon@…287…> wrote:

canvas = gcf().canvas

buf = canvas.tostring_rgb()

img = numpy.fromstring(buf, numpy.uint8)

im_size = canvas.get_width_height()

rgb = img.reshape(im_size[1], im_size[0], 3)

r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]

John Hunter wrote:

Perhaps we should make this a friendly helper method of the agg backend canvas -- canvas.toarray() returns an rgba numpy array via canvas.buffer_rgba?

+1

-Chris

···

--
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...