Pygtk: Draw lines on gtk.gdk.Pixbuf

I am using pygtk with PIL. I already understood a way to convert PIL Imageto gtk.gdk.Pixbufs. What I do to display pixbuf I create gtk.gdk.Imageand then use img.set_from_pixbuf. Now I want to draw some lines in this image. Apparently I need Drawableto do this. I started looking through documents, but I already have 8-10 windows open, and this is far from easy.

So - what magic do I need to introduce in order to get a Drawable representing my picture, draw some things on it, and then turn it into gdk.Imageso that I can display it in my application?

+3
source share
5 answers

I was doing something similar (drawing on gdk.Drawable) and I found that set_foreground was not working. To actually paint using the color I wanted, I used the following:

# Red!
gc.set_rgb_fg_color(gtk.gdk.Color(0xff, 0x0, 0x0))
+4
source

Oh my God. It hurts so much. 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)

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

+3
source

? , . .

, , - , gtk. pixbuf, , .

gdkcairocontext

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

.

+1

You must connect to the signal expose-event(GTK 2) or draw(GTK 3). In this handler, just use image.windowto get the widget gtk.gdk.Window; it is a subclass gtk.gdk.Drawable, so you can draw it.

+1
source

Source: https://habr.com/ru/post/1714071/


All Articles