This has been killing me all day. Using python and gtk, I want a gtk.gdk.Pixbuf object with text in it, created from scratch. I do not have an instance of anything screen object just yet, but I only need to make some labels. It sounded like an easy enough thing to do, there's an Image class, an abstract Drawable class, even a memory based Pixbuf class. Didn't seem a challenge...
Great bloody hell. There's two basic elements, one from the gtk.Widget tree, one from the gtk.gdk.Drawable tree, and let's not talk about graphic context and colormaps and I forget what else. To add insult to injury, you have to fire up the gtk.main to get anything and after that you're in gtk wonderland until you figure a way to escape. Trying to use an invisible window has been a bust.
My current solution stinks. It works after a fashion, but really poorly.
This page (
http://library.gnome.org/devel/pygtk/stable/ )and I currently have a hate hate relationship. This one (http://eccentric.cx/misc/pygtk/pygtkfaq.html) only tortured me with almosts. Google failed fantastically. The available material is sparse. I've started dissecting open source projects to do anything constructive with pygtk, but for this particular problem I've found no solution.
If any python gurus that have slid down the hill of razor blades into the pool of gtk alcohol can offer any ideas, links, code, etc, it would be much appreciated.
CODE
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
import pango
def GetPixbuff(text, width, height):
class MyWidget(gtk.DrawingArea):
def __init__(self, text, width, height):
gtk.DrawingArea.__init__(self)
self.connect('expose-event', self.on_expose_event)
self.layout = self.create_pango_layout(text)
self.layout.set_font_description( pango.FontDescription('Serif 12') )
self.pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 0, 8, width, height)
def on_expose_event(self, widget, event):
win = widget.window
gc = win.new_gc()
print "on_expose_event"
win.draw_layout(gc, 3, 3, self.layout)
self.pixbuf.get_from_drawable(win, win.get_colormap(), 0, 0, 0, 0, self.pixbuf.get_width(), self.pixbuf.get_height())
gtk.main_quit()
def getPixbuff(self): return self.pixbuf
widget = MyWidget(text, width, height)
win = gtk.Window()
win.add(widget)
win.show_all()
gtk.main()
return widget.getPixbuff()
if __name__ == "__main__":
pixbuf = GetPixbuff('damn snakes!', 100, 40)
pixbuf.save("foo15.jpg", "jpeg", {"quality":"100"})