Active Topics

 


Reply
Thread Tools
Posts: 437 | Thanked: 90 times | Joined on Nov 2006
#1
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's Avatar
Posts: 696 | Thanked: 1,012 times | Joined on Mar 2006 @ Asturies, Spain
#2
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.
__________________
Daniel Martín Yerga
maemo.org profile
Twitter
 

The Following 2 Users Say Thank You to yerga For This Useful Post:
Posts: 437 | Thanked: 90 times | Joined on Nov 2006
#3
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!)
 

The Following 3 Users Say Thank You to convulted For This Useful Post:
daperl's Avatar
Posts: 2,427 | Thanked: 2,986 times | Joined on Dec 2007
#4
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:

Name:  screenshot24.jpg
Views: 3643
Size:  22.7 KB
Attached Files
File Type: gz pixbufs.tar.gz (122.2 KB, 343 views)
__________________
N9: Go white or go home

Last edited by daperl; 2009-11-29 at 18:19.
 

The Following User Says Thank You to daperl For This Useful Post:
Posts: 437 | Thanked: 90 times | Joined on Nov 2006
#5
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
 
Reply


 
Forum Jump


All times are GMT. The time now is 02:01.