4

I'm using pygtk with PIL. I've already figured out a way to convert PIL Images to gtk.gdk.Pixbufs. What I do to display the pixbuf is I create a gtk.gdk.Image, and then use img.set_from_pixbuf. I now want to draw a few lines onto this image. Apparently I need a Drawable to do that. I've started looking through the docs but already I have 8-10 windows open and it is not being straightforward at all.

So - what magic do I need to type to get a Drawable representing my picture, draw some things on to it, and then turn it into a gdk.Image so I can display it on my app?

Claudiu
  • 224,032
  • 165
  • 485
  • 680

5 Answers5

4

I was doing something similar (drawing to a gdk.Drawable), and I found that set_foreground doesn't work. To actually draw using the color I wanted, I used the following:

# Red!
gc.set_rgb_fg_color(gtk.gdk.Color(0xff, 0x0, 0x0))
theY4Kman
  • 5,572
  • 2
  • 32
  • 38
3

Oh my god. So painful. Here it is:

    w,h = pixbuf.get_width(), pixbuf.get_height()
    drawable = gtk.gdk.Pixmap(None, w, h, 24)
    gc = drawable.new_gc()
    drawable.draw_pixbuf(gc, pixbuf, 0, 0, 0, 0, -1, -1)

    #---ACTUAL DRAWING CODE---
    gc.set_foreground(gtk.gdk.Color(65535, 0, 0))
    drawable.draw_line(gc, 0, 0, w, h)
    #-------------------------

    cmap = gtk.gdk.Colormap(gtk.gdk.visual_get_best(), False)
    pixbuf.get_from_drawable(drawable, cmap, 0, 0, 0, 0, w, h)

It actually draws a black line ATM, not a red one, so I have some work left to do...

Claudiu
  • 224,032
  • 165
  • 485
  • 680
1

Have you tried cairo context? sorry i seem cant comment on your post. hence i posted it here.

I haven't tried this myself but I believe that cairo is your friend when it comes to drawing in gtk. you can set the source of your cairo context as pixbuf so i think this is usable for you.

gdkcairocontext

b3rx
  • 366
  • 1
  • 2
  • 8
1
image = gtk.Image()
pixmap,mask = pixbuf.render_pixmap_and_mask() # Function call
cm = pixmap.get_colormap()
red = cm.alloc_color('red')
gc = pixmap.new_gc(foreground=red)
pixmap.draw_line(gc,0,0,w,h)
image.set_from_pixmap(pixmap,mask)

Should do the trick.

Doug Blank
  • 2,031
  • 18
  • 36
1

You should connect to the expose-event (GTK 2) or draw (GTK 3) signal. In that handler, simply use image.window to get the widget's gtk.gdk.Window; this is a subclass of gtk.gdk.Drawable, so you can draw on it.

ptomato
  • 56,175
  • 13
  • 112
  • 165