[pygtk] GdkPixdata in PyGTK?

Tim Evans t.evans at aranz.com
Thu Jun 16 07:12:39 WST 2005


César Leonardo Blum Silveira wrote:
> Hello all,
> 
> I think this is my first post to the list, but I've been reading it
> for a while now.
> When using GTK in C, there is a data type called GdkPixdata. It's a
> structure that contains information about an image, like width and
> height, and also a buffer with lots of rgb values.
> I couldn't find anything similar in PyGTK, however. Basicly, what I
> want to know is if there is a way to store lots of rgb values and x
> and y coordinates on some kind of structure and then convert it into a
> GdkPixbuf and then display it.
> 
> I'll use that to visualize the results of another program, that
> outputs a file like the following:
> 
> X Y R G B
> X Y R G B

Starting from the inputs:
  - 'w':    the total width of the image.
  - 'h':    the total height of the image.
  - 'data': a list of (x,y,r,g,b) where r, g, and b are integers in the
            range [0,255].
You could use Numeric to create the image:

   import Numeric
   import gtk

   array = Numeric.zeros((h,w,3), 'b')
   for (x,y,r,g,b) in data:
       array[y,x,:] = (r, g, b)
   pixbuf = gtk.gdk.pixbuf_new_from_array(array,
                                          gtk.gdk.COLORSPACE_RGB, 8)

This will leave the pixels that aren't mentioned in 'data' black.  If 
you want them to be clear (alpha of zero) instead:

   import Numeric
   import gtk

   array = Numeric.zeros((h,w,4), 'b')
   for (x,y,r,g,b) in data:
       array[y,x,:] = (r, g, b, 255)
   pixbuf = gtk.gdk.pixbuf_new_from_array(array,
                                          gtk.gdk.COLORSPACE_RGB, 8)

-- 
Tim Evans
Applied Research Associates NZ
http://www.aranz.com/


More information about the pygtk mailing list