From: Nicolas <nic...@ya...> - 2007-06-28 14:57:00
|
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 |
From: Lionel R. <lro...@li...> - 2007-06-28 15:13:47
|
Le jeudi 28 juin 2007, Nicolas a =C3=A9crit=C2=A0: > Hi, > > I would like to export a zone of a Figure in .png. > Something like figure.savefig("mypicture.png", box =3D (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. =2D-=20 Lionel Roubeyrie - lro...@li... Charg=C3=A9 d'=C3=A9tudes et de maintenance LIMAIR - la Surveillance de l'Air en Limousin http://www.limair.asso.fr |
From: Nicolas <nic...@ya...> - 2007-06-28 15:24:24
|
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 |
From: John H. <jd...@gm...> - 2007-06-28 16:22:19
|
On 6/28/07, Nicolas <nic...@ya...> 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 |
From: Nicolas <nic...@ya...> - 2007-06-29 13:59:37
|
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 <jd...@gm...> wrote: > > On 6/28/07, Nicolas <nic...@ya...> 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 > |
From: Christopher B. <Chr...@no...> - 2007-07-02 21:57:04
|
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 Chr...@no... |
From: Nicolas <nic...@ya...> - 2007-07-03 08:32:40
|
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 <Chr...@no...> 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 > > Chr...@no... > |
From: Christopher B. <Chr...@no...> - 2007-07-03 17:47:41
|
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 Chr...@no... |
From: Christopher B. <Chr...@no...> - 2007-07-05 17:09:29
|
Nicolas wrote: > > as I plan to make a Qt version in the future) > good idea. > You mean, to use Qt instead of Wx ? No, I'm a big wx fan. What I meant was that it's a good idea to keep this functionality GUI toolkit independent. > I've found a possible solution : looks good to me. > buffer = self.get_renderer().buffer_rgba(0,0) # better > because alpha should be the last value Why is that? Is it the "native" backend_agg format? -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 Chr...@no... |