export of a specific zone of a figure

Hi,

I would like to export a zone of a Figure in .png.
Something like figure.savefig(“mypicture.png”, box = (0,0,5,5))
How may I proceed, without drawing all the plots again ?
I use wxagg.
Thanks,

Nicolas

···

Ne gardez plus qu’une seule adresse mail ! Copiez vos mails vers Yahoo! Mail

Look at PIL, you have normaly a such function there.

···

Le jeudi 28 juin 2007, Nicolas a écrit :

Hi,

I would like to export a zone of a Figure in .png.
Something like figure.savefig("mypicture.png", box = (0,0,5,5))
How may I proceed, without drawing all the plots again ?
I use wxagg.
Thanks,

Nicolas

---------------------------------
Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers Yahoo!
Mail

--
Lionel Roubeyrie - lroubeyrie@...1068...
Chargé d'études et de maintenance
LIMAIR - la Surveillance de l'Air en Limousin
http://www.limair.asso.fr

Thanks for your reply.

However, I don’t want to had a PIL dependency.

Is there any other method, using only matplotlib or wx ?

Nicolas

agg offers methods to convert the image pixel buffer to strings or
buffers, which you could then convert to numpy arrays, so a slice
extraction, and reconvert back to a buffer and ultimately a PNG. I
don't have time right now to write some example code, but you may want
to poke around in backend_agg to see if you can figure it out, and if
not remind me next week.

Thanks,
JDH

···

On 6/28/07, Nicolas <nico_75_0@...136...> wrote:

Thanks for your reply.

However, I don't want to had a PIL dependency.

Is there any other method, using only matplotlib or wx ?

Hi,
I can figure the first steps :
something like :

        matrix = []
        buffer = self.get_renderer().tostring_argb()
        l, h = self.GetSize()
        for ligne in xrange(h):

            matrix.append([])
            for colonne in xrange(l):
                i = 4*(ligne*h + colonne)
                pixel = buffer[i:i+4]
                matrix[-1].append(pixel)

        zone_to_export = array(matrix)[pixely0:pixely1, pixelx0:pixelx1]
        new_buffer = buffer("".join("".join(elt for elt in ligne) for ligne in zone_to_export ))

But then I don’t know what to with this new buffer.

I tried to create a new RenderAgg instance, so as to use its png export facilities.
r = RendererAgg(pixelx1 - pixelx0, pixely1 - pixely0, Value(dpi))

r._renderer.write_png(nom)

But I don’t know what to put between the two previous lines.

How may I load a buffer content into a RenderAgg instance ?
I suppose I may use something like :
r.draw_image(0, 0, im)
but what is the correct format for im ? Is there an Image class in matplotlib (I looked for, but didn’t find).

How may I convert my buffer ?

Thanks a lot,

Nicolas

How may I transform my buffer into an image ?

···

On 6/28/07, John Hunter < jdh2358@…287…> wrote:

On 6/28/07, Nicolas < > nico_75_0@…136…> wrote:

Thanks for your reply.

However, I don’t want to had a PIL dependency.

Is there any other method, using only matplotlib or wx ?

agg offers methods to convert the image pixel buffer to strings or
buffers, which you could then convert to numpy arrays, so a slice
extraction, and reconvert back to a buffer and ultimately a PNG. I
don’t have time right now to write some example code, but you may want

to poke around in backend_agg to see if you can figure it out, and if
not remind me next week.

Thanks,
JDH

I don't know how to do it with the MPL agg back-end, but I think you mentioned wx, and you can do it there instead. a wxImage can be constructed from a buffer object, then saved as a PNG. You may need to set the rgb and alpha portions separately. See the wxPython wiki and search for "Image".

Also:

            matrix =
            buffer = self.get_renderer().tostring_argb()
            l, h = self.GetSize()
            for ligne in xrange(h):
                matrix.append()
                for colonne in xrange(l):
                    i = 4*(ligne*h + colonne)
                    pixel = buffer[i:i+4]
                    matrix[-1].append(pixel)

This is a very slow way to create the numpy array!

Option a: first create an empty array:

matrix = numpy.empty((l,h,4), numpy.byte)

then fill that in. but even better:

you can build the array directly from the buffer string:

matrix = numpy.fromstring(buffer, dtype=numpy.byte)
lotlib-users

···

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

Thank you very much.
I know very little about numpy in fact.

If I don’t find a pure matplotlib method, I will use your suggestion with wx.
I think however matplotlib may be used only (and it will be even better as I plan to make a Qt version in the future)

So, in :

from matplotlib.transforms import Value
from matplotlib.backends.backend_agg import RendererAgg
r = RendererAgg(50, 50, Value(72))
r.draw_image
(0, 0, im)

What is the correct format for im ?

Thanks,

Nicolas

···

On 7/2/07, Christopher Barker < Chris.Barker@…259…> wrote:

I don’t know how to do it with the MPL agg back-end, but I think you

mentioned wx, and you can do it there instead. a wxImage can be
constructed from a buffer object, then saved as a PNG. You may need to
set the rgb and alpha portions separately. See the wxPython wiki and
search for “Image”.

Also:

        matrix = []
        buffer = self.get_renderer().tostring_argb()
        l, h = self.GetSize()
        for ligne in xrange(h):
            matrix.append([])
            for colonne in xrange(l):
                i = 4*(ligne*h + colonne)
                pixel = buffer[i:i+4]
                matrix[-1].append(pixel)

This is a very slow way to create the numpy array!

Option a: first create an empty array:

matrix = numpy.empty((l,h,4), numpy.byte)

then fill that in. but even better:

you can build the array directly from the buffer string:

matrix = numpy.fromstring(buffer, dtype=numpy.byte)
lotlib-users


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…

Nicolas wrote:

I think however matplotlib may be used only (and it will be even better as I plan to make a Qt version in the future)

good idea.

So, in :
>>> from matplotlib.transforms import Value
>>> from matplotlib.backends.backend_agg import RendererAgg
>>> r = RendererAgg(50, 50, Value(72))
>>> r.draw_image (0, 0, im)

What is the correct format for im ?

I'm no expert, but probably a string that's the same format as what tostring_argb() returns, so something like this should work (untested!):

buffer = self.get_renderer().tostring_argb()
l, h = self.GetSize()
matrix = numpy.fromstring(buffer, dtype=numpy.byte)
matrix.shape = (l,h,4) # 4 for a,r,g,b
sub_matrix = matrix[min_x:max_x, min:y_max_y, :]
r.draw_image (0, 0, sub_matrix.tostring())

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