[cairo] pycairo and PIL

Steve Chaplin stevech1097 at yahoo.com.au
Wed Sep 21 21:40:09 PDT 2005

On Wed, 2005-09-21 at 16:10 -0500, James Evans wrote:
> I've been using pycairo to do some simple graphics for a project that
> I need to access the image in PIL. Rather than using
> surface.write_to_png() and then reading the image file into PIL I
> added a get_data() method to the surface object that returns the data
> in Image.frombuffer() compatible format.
> I think this is an important piece of functionality to make cairo
> useful to other python toolkits. I think this could be used as the
> basis for a wxCairoDC to provide the power of cairo to wxPython based
> apps.

Are you saying that to get wxPython to work with pycairo you should use
PIL? I don't know why PIL would be needed and think it would be better
to connect wxPython directly to pycairo, using a method similar to the
way pygtk connects to pycairo. But I don't know the specific details as
I don't use wxPython.

> With this patch you can create a PIL image from a cairo surface with:
> import Image
> newimage = Image.frombuffer("RGBA", (surface.get_width(),
> surface.get_height()), surface.get_data(), "raw", "RGBA", 0,1)

Cairo and pycairo being low-level graphics libraries that higher-level
libraries can require and build upon. So I think that PIL (or any other
module) could check if pycairo is installed and optionally build
functions to interface to pycairo, rather than the other way round.
So in this case PIL could provide a "get_data(surface)" or "Image.from
cairosurface()" function.

> james evans


Send instant messages to your online friends http://au.messenger.yahoo.com 

More information about the cairo mailing list