2011-08-23
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(
'eNrdl1EOgDAIQ73/nTib+r3AKKXMRBPiD/pgA7qZ2WWgvc+9mhW+Z81jTvK9PFfWBB/hnuQj/lNrj/'
'p+zfd8O3vT4at6s8qv1K2CP8VF9zViqmLZzZuMp+oJJCckFiamKNdKrIq5u/vfrvcqfZzVUpQ7UqNM'
'vyF7PXlGQNaj0j/MWnS1l82/qv9ZLXbzZ2d2d/Z5b0YHFXMP0SXUT8HP8lRpL1L/0fxR6E6ndqd1T9'
'Uz6NmH0adT96IT3D/bA2j1XFI='
)), 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.
