[cairo] pycairo and PIL

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

I've looked through one half of the patch - the
cairo.ImageSurface.create_for_data() part.

The problem is that we need to make sure the 'data' passed to
cairo_image_surface_create_for_data() is not deallocated before we have
finished using the cairo surface.
I do not think that calling PyBuffer_FromReadWriteMemory() and
referencing 'buffer' ensures the underlying 'data' is not deallocated.

I think the original error was to use
    if (!PyArg_ParseTuple(args, "w#iii|i:Surface.create_for_data",
which gives us the internal buffer data, but not the python object.
A better method is to read a Python object, call
PyObject_AsWriteBuffer, and reference the python object so the internal
data is not deallocated.

I've updated cvs use this new method and updated the test script, which
seems to be working OK.


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

More information about the cairo mailing list