[cairo] pycairo and PIL

Steve Chaplin stevech1097 at yahoo.com.au
Thu Sep 22 19:11:23 PDT 2005


On Thu, 2005-09-22 at 07:39 -0500, James Evans wrote:
> > 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.
> 
> I guess I didn't explain that one very well. I'm not thinking that
> wxPython needs PIL to use cairo, but that the same methods that are useful
> for PIL can be useful for wxPython.

wxWidgets on Unix uses GTK+, and GTK+ 2.8 now uses cairo. So if
wxWidgets were to expose these GTK+ cairo functions then wxPython could
create the Python wrappers to give wxPython users access to cairo. This
would interface directly to the cairo Xlib backend rather than using
cairo ImageSurface memory buffers.

> > 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.
> 
> This was exactly my problem. How does PIL get access to the data in a
> cairo surface? The only exposed interface is by giving a numpy array to
> cairo to use as a surface. Numpy is a large addition that in my
> understanding (I've not used it extensively) suffers from performance
> problems. What I was trying to do was to provide a python program direct
> access to the data contained in the cairo surface. If pycairo doesn't
> expose an API for accessing this, how is the python program going to get
> at it?

I agree that having a surface.get_data() method would be useful, the
point I was trying to make is does the code belong in PIL or pycairo? If
it would be widely used then its best placed in pycairo, but if its of
specific use to PIL then it should probably go in PIL.
pycairo exposes a C API that allows other modules to write extension
code to access pycairo objects. pygtk uses the pycairo C API to provide
cairo support.

> I've only used cairo for a week or so, so it is very possible I missed
> something here, but I didn't see any way to access the surface's data
> (once again, excepting numpy). Is there a better way to get the data?

You could use PIL to read/write to png, and use pycairo 
  surface = cairo.ImageSurface.create_from_png(file)
  surface.write_to_png(file)
to read/write to png, where 'file' is a filename or a file object.

This may be easier than reading to/from a string or python buffer since
it should bypass the image format problems.

> I know that undoing the premuliplication isn't the best way to pass the
> data back, it just suits what I'm doing with it at this time. I'd think it
> should be an option.
> 
> Thanks,
> 
> james evans

Steve

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


More information about the cairo mailing list