Stumbling Toward 'Awesomeness'

A Technical Art Blog

Friday, October 17, 2014

Embedding Icons and Images In Python with XPM


As technically-inclined artists, we often like to create polished UIs, but we have to balance this with not wanting to complicate the user experience (fewer files the better). I tend to not use too many icons with my tools, and my Maya tools often just steal existing icons from Maya: because I know they will be there.

However, you can embed bitmap icons into your PySide/PyQt apps by using the XPM file format, and storing it as a string array. Often I will just place images at the bottom of the file, this keeps icons inside your actual tool, and you don’t need to distribute multiple files or link to external resources.

Here’s an example XPM file:

/* XPM */
static char *heart_xpm[] = {
/* width height num_colors chars_per_pixel */
"7 6 2 1",
/* colors */
"` c None",
". c #e2385a",
/* pixels */

This is a small heart, you can actually see it, in the header you se the ‘.’ maps to pink, you can see the ‘.’ pattern of a heart. The XPM format is like C, the comments tell you what each block does.
Here’s an example in PySide that generates the above button with heart icon:

import sys
from PySide import QtGui, QtCore
def main():
    app = QtGui.QApplication(sys.argv)
    heartXPM = ['7 6 2 1','N c None','. c #e2385a','N..N..N',\
    w = QtGui.QWidget()
    w.setWindowTitle('XPM Test')
    w.button = QtGui.QPushButton('XPM Bitmaps', w)
if __name__ == '__main__':

You need to feed QT a string array, and strip everything out. Gimp can save XPM, but you can also load an image into xnView and save as XPM.

Addendum: Robert pointed out in the comments that pyrcc4, a tool that ships with PyQt4, can compile .qrc files into python files that can be imported. I haven’t tried, but if they can be imported, and are .py files, I guess they can be embedded as well. He also mentioned base64 encoding bitmap images into strings and parsing them. Both these solutions could really make your files much larger than XPM though.

posted by Chris at 3:23 PM  


  1. if I remember correctly, you can also use PyQt’s resource compiler (pyrcc4) to compile bitmaps and other resources to .py files. The files will be longer than with the XPM approach, but you can use different formats with it.

    If you’re not using Qt, then you may just get away by base64 encoding anything binary and putting it in a string in your .py file, as long as it’s not too much data.

    Comment by robert — 2014/10/17 @ 6:37 PM

  2. Cool, thanks Robert, I updated the post. I didn’t know about that one!

    Comment by admin — 2014/10/17 @ 7:36 PM

  3. Thanks for posting this! I’ve been making a file management UI for our studio and this worked perfectly. It’s nice to have the info embedded in the python files so I don’t have mess around with extra image files.

    Comment by Josh — 2015/03/31 @ 10:15 PM

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress