sending plot output to a bitmap widget

But it does make sense to have a general function to read

    > out an AGG rendered buffer to a Numeric/numarray array
    > doesn't it? Seems like another good thing to add to the
    > list (unless one can do that already, I forget the details
    > 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()

John Hunter wrote:

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.

It's not a strong driver for us at the moment, but I agree that
it depends on how people see using this capability. For our part
(and this is just limited to my knee-jerk reaction, there may
be other uses that I can't think of but others may) I would guess
the most common use would be to create some sort of image with
overlays (contours, legends, etc) that one wants to write out
to one of the scientific data formats that are used to store
scientific images. Normally these would be array-aware and so
arrays make a good intermediary. Perhaps the values would be
manipulated as well as arrays but that seems less likely.
I'll see if I can conceive of any other uses. Saving memory
copying doesn't seem to be that important for these sorts of
uses since they would not be frequent, nor involve gigantic
images that couldn't stand to be copied.

An associated question is how one easily maps a monochromatic
image that is displayed in matplotlib back into a monochromatic
array. Normally it's going to be converted to rgba even if rgba
is not really needed. Is there any simple way of mapping it back
to intensity (histogram of the rgba values to determine if such
a mapping exists?). Hmmm.

Perry