I'm using pygtk with PIL. I've already figured out a way to convert PIL Image
s to gtk.gdk.Pixbuf
s. 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?
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))
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...
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.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.