maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Drawing on gtk.Image (https://talk.maemo.org/showthread.php?t=35249)

convulted 2009-11-27 13:55

Drawing on gtk.Image
 
Hello all,

I apologise for asking a basic question. Suppose I have the code:

Code:

PixBuf = gtk.gdk.pixbuf_new_from_file("image.png")
Image = gtk.image_new_from_pixbuf(PixBuf)

(I then add the image to the Hildon window in the "standard" manner),
and suppose I want to draw a rectangle (say) on top of the displayed PNG. How do I get round to doing this?

Thanks!

yerga 2009-11-29 13:38

Re: Drawing on gtk.Image
 
I think you need a gtk.DrawingArea, AFAIK it's not possible draw directly in a gtk.Image (I could be wrong though).

You can check Mirage source code (search the "crop_image" function), in the cropping functions it draws a rectangle on top of a image that's stored in a DrawingArea. It's a bit difficult to understand this code if it's the first time that you play with a DrawingArea but probably you'll be able to purge most of the code in those Mirage functions.

convulted 2009-11-29 14:01

Re: Drawing on gtk.Image
 
Hello, and thanks for finding the time to reply. I managed to draw on the image by: loading it as a pixbuf, getting the pixmap from that pixbuf, drawing on the pixmap and then creating a gtk.Image from the pixmap. Not elegant (horribly inelegant in fact) but seems to work. If only people documented this sort of thing better!

Thus, my (possibly optimisable) code reads:
Code:

pixbuf = gtk.gdk.pixbuf_new_from_file("image.png")
pixmap,mask = pixbuf.render_pixmap_and_mask()
cm = pixmap.get_colormap()
red = cm.alloc_color('red')
gc = pixmap.new_gc(foreground=red)
pixmap.draw_rectangle(gc, False, x, y, w, h)
image = gtk.Image()
image.set_from_pixmap(pixmap, mask)

(Just in case anyone has the same problem... it took me hours to find the solution online!)

daperl 2009-11-29 18:14

Re: Drawing on gtk.Image
 
2 Attachment(s)
Thanks for the code.

A gtk.Image is a gtk.Widget, and gtk.Widget's have the attribute "window." "window," if not none, is the gtk.gdk.Window of the widget. gtk.gdk.Window and gtk.gdk.Pixmap are both gtk.gdk.Drawables. So, for gtk.Images you can do the following after you've realized them in a container:

Code:

aWindow = gtk.Window()
...
myImage = gtk.Image()
myImage.set_from_file('myFile.jpg')
aWindow.add(myImage)
cm = myImage.window.get_colormap()
gc = myImage.window.new_gc(foreground=cm.alloc_color('#ff0000',True,False))
myImage.window.draw_rectangle(gc, False, x, y, width, height)

Pixmaps aren't always needed and this introduces the concept of overlays. The rectangle isn't actually part of the image. The rectangle in your example is. So, I guess I'm saying make sure you're using the right tool for the job. See my attachment for a PyGTK demo I fixed up for n8x0's that is a good example of animation overlaying. Here's a screenshot:

Attachment 4813

convulted 2009-11-29 19:03

Re: Drawing on gtk.Image
 
Thanks for that! If there's one objection I have towards python it's the quality of documentation available, especially where GTK is involved (yes, yes I know they're separate projects of course, but someone ported the GTK libraries and therefore -- presumably -- he/she cares enough; or maybe I haven't looked hard enough). Anyway, my Image is inside an EventBox inside a Hildon PannableArea, and Image.window is set to None.

Eventually, I'll do this properly, but my aim is for Tubes 0.2.0-1 to "just work" and not be elegant under the hood. That might have to wait until 1.0.0 :)


All times are GMT. The time now is 04:24.

vBulletin® Version 3.8.8