From: John H. <jdh...@ac...> - 2004-04-29 17:18:08
|
>>>>> "Perry" == Perry Greenfield <pe...@st...> writes: Perry> But it does make sense to have a general function to read Perry> out an AGG rendered buffer to a Numeric/numarray array Perry> doesn't it? Seems like another good thing to add to the Perry> list (unless one can do that already, I forget the details Perry> of using AGG by itself). You can do this with string methods already. The question is: is it worth adding a to_numerix and/or from_numerix method for performance and convenience to directly access the agg pixel buffer representing the entire figure canvas. The (smallish) downside of the latter is that currently backend_agg does not depend on Numeric/numarray at all. There are a couple of ways to go here and I think it would be useful to know how people would want to use this functionality. In the example below, I show how to get the agg pixel buffer as an rgb string and either convert it to an array or pass it to PIL. I need to add the tostring_rgba equivalent - I added tostring_rgba this as a helper for backend ps which doesn't need alpha. If all that is needed is a way to get at the contents as an array, then it may be enough to wrap the example code below in a helper function or appropriate class method. But if one wants to be able to directly manipulate the contents of the agg rgba pixel array using numeric and then pass this back to agg, more will be needed. So knowing what people need this for, and how critical performance issues will be in these cases, is important in deciding which tack to take. """ Use backend agg to access the figure canvas as an RGB string and then convert it to a Numeric array and pass the string it to PIL for rendering """ from matplotlib.matlab import * from matplotlib.backends.backend_agg import FigureCanvasAgg plot([1,2,3]) canvas = get_current_fig_manager().canvas agg = canvas.switch_backends(FigureCanvasAgg) agg.draw() s = agg.tostring_rgb() # get the width and the height to resize the matrix l,b,w,h = agg.figure.bbox.get_bounds() w, h = int(w), int(h) X = fromstring(s, UInt8) X.shape = h, w, 3 import Image im = Image.fromstring( "RGB", (w,h), s) im.show() |