Embedding images in Python code

Today I wrote a relatively trivial tool, to solve a relatively trivial problem: I wanted to be able to embed small images directly in the source-code of a python (PyGTK) program.

PyGTK already knows how to do that sort of thing, more or less, using the old XPM textual format. However, XPM has a few historic limitations, most notably being a lack of a proper alpha channel. It's also rather verbose - which is nice if you want to tweak your images with a text editor, but annoying if you just want to embed a logo.

So I wrote img to pygtk.py!

Use and output look something like this:

$ img_to_pygtk.py bre_logo.gif
PIXBUF = gtk.gdk.pixbuf_new_from_data(zlib.decompress(base64.b64decode(
    )), gtk.gdk.COLORSPACE_RGB, True, 8, 32, 31, 128)

It should be able to handle pretty much any image format suppoted by PyGTK.

I'll probably end up adding similar functionality to PyBreeder.

Tags: tech

Recent posts