[cairo] pycairo and PIL

Steve Chaplin stevech1097 at yahoo.com.au
Thu Sep 22 19:08:22 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.

I think this would be a Python solution to a C/C++ problem, and it would
be much better if the underlying C/C++ libraries (wxWidgets and cairo)
interfaced directly, and then wxPython would just need to 'wrap' the
wxWidgets functions that provide access to cairo.

> 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)
> 
> (I don't know why I need the 1 for the orientation, it's supposed to be
> the default, but the image is upside down without it.)
> 
> This is a quick-and-dirty patch to add the functionality. Suggestions and
> changes are welcome!
> 
> Thanks,
> 
> james evans
> 
> 
> _______________________________________________
> cairo mailing list
> cairo at cairographics.org
> http://cairographics.org/cgi-bin/mailman/listinfo/cairo

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


More information about the cairo mailing list