views:

1121

answers:

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?

A: 

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
+1  A: 

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
+1  A: 

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 had to use the following:

# Red!
gc.set_rgb_fg_color(gtk.gdk.Color(0xff, 0x0, 0x0))
theY4Kman
A: 
image = gtk.Image()
pixmap,mask = pixbuf.render_pixmap_and_mask
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.