[cairo] pycairo and PIL

James Evans milamber at clark-evans.com
Wed Sep 21 14:10:09 PDT 2005


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.

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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: pycairo.image.patch
Type: text/x-patch
Size: 4801 bytes
Desc: not available
Url : http://lists.freedesktop.org/archives/cairo/attachments/20050921/6d508db1/pycairo.image.bin


More information about the cairo mailing list