[pygtk] Drawing on a gdk.Pixbuf using Cairo.

Osmo Maatta osmoma at gmail.com
Tue Mar 2 17:59:05 WST 2010


Hi again,

I have a program that needs to draw circles and other elements on a 
gdk.Pixbuf image.

The current code works well but when creating a Cairo-surface from 
pixbuf, the  gtk.gdk.Pixbuf.get_pixels_array() function reports that 
it's deprecated and should be avoided.

pix_data = pixbuf.get_pixels_array()
surface = cairo.ImageSurface.create_for_data(pix_data, 
cairo.FORMAT_RGB24, pixbuf.get_width(), 
                                                                            
pixbuf.get_height(), pixbuf.get_rowstride())
The message says:
"""draw-test.py:29: DeprecationWarning: PyArray_FromDimsAndDataAndDescr: 
use PyArray_NewFromDescr.
pix_data = pixbuf.get_pixels_array()"""

Ref: 
http://www.pygtk.org/docs/pygtk/class-gdkpixbuf.html#method-gdkpixbuf--get-pixels-array

Here is my test code.
http://www.futuredesktop.com/tmp/draw-test.py
-----

The c version of Cairo and PyCairo behave differently.
In c code,  to get create the Cairo surface, I have to use 
gdk_pixbuf_get_pixels(), not the get_pixels_array() function.

    gint pixbuf_width = gdk_pixbuf_get_width(pixbuf);
    gint pixbuf_height = gdk_pixbuf_get_height(pixbuf);

    cairo_surface_t *surface = 
cairo_image_surface_create_for_data(gdk_pixbuf_get_pixels(pixbuf),
                                          CAIRO_FORMAT_RGB24,
                                          pixbuf_width,
                                          pixbuf_height,
                                          gdk_pixbuf_get_rowstride(pixbuf));

Unfortunately PyCairo's
surface = cairo.ImageSurface.create_for_data(...) expects an array type, 
not a string type. AFAIK, strings are not arrays in Python.

http://www.pygtk.org/docs/pygtk/class-gdkpixbuf.html#method-gdkpixbuf--get-pixels
returns a string type
------

Questions:
1) What is the best way to draw onto gdk.Pixbuf? (most likely using 
Cairo, other means also welcomed)

2) If  the gtk.gdk.Pixbuf.get_pixels_array() or its internals are 
deprecated, then what will replace it?

Greetings
  Osmo (Moma) Antero
  Grønland, Oslo

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20100302/68cfd44a/attachment.htm 


More information about the pygtk mailing list