pygtk: Draw lines onto a gtk.gdk.Pixbuf

▼魔方 西西 提交于 2019-12-06 06:54:29

问题


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?


回答1:


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))



回答2:


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...




回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/1213090/pygtk-draw-lines-onto-a-gtk-gdk-pixbuf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!