From: Friedrich R. <fri...@gm...> - 2010-03-28 19:35:33
|
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 |