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.l...@tuebingen.mpg.de> 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.l...@tuebingen.mpg.de
> 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
>

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

Reply via email to