From: Filipe P. A. F. <oc...@gm...> - 2010-03-30 16:31:42
|
Thanks Friedrich, However, my knowledge of python is very limited, even though I think I understood what you suggested I do not know how to get the shape (of the figure?) for this part: >>> fig.set_size_inches(float(shape[0]) / dpi, float(shape[1]) / dpi) error is: Traceback (most recent call last): File "blueearth-map.py", line 108, in <module> fig.set_size_inches(float(shape[0]) / dpi, float(shape[1]) / dpi) TypeError: 'function' object is unsubscriptable It seems that it ended up calling a function shape instead of a variable shape. I'm sending attached the script if you are interested in looking at it. Thanks again. Filipe. On Sun, Mar 28, 2010 at 3:35 PM, Friedrich Romstedt <fri...@gm...> wrote: > > 2010/3/28 Filipe Pires Alvarenga Fernandes <oc...@gm...>: > > Hello list > > I've trying for a while a "python only" solution to remove white spaces that > > Basemap generate to keep the aspect ratio. I found these two threads that > > explain the issue better: > > I think maybe you can make use of the Agg Backend to achieve a > Python-only solution to obtain a PIL Image from a figure without > bypassing over the HDD: > > Furthemore, if this doesn't work, maybe you can use a StringIO as > "file", then seek() the StringIO and reread with PIL from the > "file-like" StringIO, or something like that? > > # > # Render the Figure to a PIL Image ... > # > > # Prepare figure to be of pixel extent *shape* ... > > dpi = figure.dpi > figure.set_size_inches(float(shape[0]) / dpi, float(shape[1]) / dpi) > > # Create the rendering Canvas ... > # > # We also convert the picture to an RGB string. > > canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(figure) > canvas.draw() > image_string = canvas.tostring_rgb() > > # Create a PIL Image from RGB string ... > > image = Image.fromstring("RGB", shape, image_string) > > # Now do whatever you want with the Image. > > Friedrich |