| FAQ Index - Search - Recent Changes - Everything - Add entry |
8.18. How blend (composite) two images together?
You should use the method 'composite' of gtk.gdk.Pixbuf. It takes a bunch of parameters. The first parameter is both the second pixbuf to composite with the first one, and also the destination where the resulting image will be stored (the original pixbuf image data is destroyed in the process). The last parameter is a transparency level, from 0 to 255, where 127 means to blend the images 50% from each source.
Obligatory example:
pixbuf = gtk.gdk.pixbuf_new_from_file("foo.png")
pixbuf2 = gtk.gdk.pixbuf_new_from_file("bar.png")
pixbuf.composite(pixbuf2, 0, 0, pixbuf.props.width, pixbuf.props.height, 0, 0, 1.0, 1.0, gtk.gdk.INTERP_HYPER, 127)
## now pixbuf2 contains the result of the compositing operation
pixbuf2.save("zbr.png", 'png')
Beware that the destination image has to be at least as large as the first image. Otherwise you might get a cryptic warning like:
python foo.py foo.py:7: GtkWarning?: gdk_pixbuf_composite: assertion `dest_x >= 0 && dest_x + dest_width <= dest->width' failedYou can also do this using just cairo: (PyGTK 2.8+)
import cairo
s1 = cairo.ImageSurface.create_from_png('foo.png')
s2 = cairo.ImageSurface.create_from_png('bar.png')
ctx = cairo.Context(s1)
ctx.set_source_surface(s2, 0, 0)
ctx.paint()
s1.write_to_png('result.png')
