On Wed, 2005-08-24 at 12:58 +0200, Jesper Larsen wrote:
> CherryPy is able to serve/stream the image directly to the browser if it is
> converted to a string using an appropriate encoding (png, rgb maybe). PIL is
> able to do this conversion:
>
> http://www.theorganization.net/mt/archives/2005/05/30/ive_finally_got_it.html
>
> I would like to do something similar with my matplotlib image (i.e. convert it
> to a string with an appropriate encoding) instead of writing it to the
> harddisk. Does anyone know if that is possible?
I've just been working on something similar (although not for web use)
myself and came up with the following solution although it's possible
there's a better way.
Using the agg backend you can obtain an RGBA buffer or RGB string which
can then be loaded as a PIL Image for processing. I've adapted a the
examples/agg_oo.py to demonstrate.
----
from matplotlib.backends.backend_agg \
import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import Image
fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.plot([1,2,3])
ax.set_title('hi mom')
ax.grid(True)
ax.set_xlabel('time')
ax.set_ylabel('volts')
canvas.draw()
size = canvas.get_width_height()
usebuffer = True
if usebuffer:
# Load the agg buffer directly as the source of the PIL image
# - could be less stable as agg and PIL share memory.
buf = canvas.buffer_rgba()
im = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
else:
# Save the agg buffer to a string and load this into the PIL image.
buf = canvas.tostring_rgb()
im = Image.fromstring('RGB', size, buf, 'raw', 'RGB', 0, 1)
im.show()
----
Hope this helps,
Nick
|