Roland's homepage

My random knot in the Web

Including binary data in Python scripts

Sometimes I need to use some binary data (e.g. a bitmap image) in a Python script. In a package, you can store this data portable in a data subdirectory of a modules using the package_data argument of setuptools.setup. This won’t work in a standalone script. You can of course make your script into a package, but here I want to show an alternative solution.

Preparing the data

For example, let’s start with this image:

Python powered logo

This is an 8-bit RGBA image. Since logo’s like this typically have few colors, we can make it smaller by converting it to a colormapped image.

convert python-powered-w-200x80.png PNG8:python-powered-w-200x80-cmap.png

This reduces the file size from 8758 bytes to 2483 bytes.

For non-image binary data, it might make sense to compress it with one of the compression formats that Python can use natively, like zlib, bz2 or lzma.

Converting to Python

Using the following code, we can convert the image or data file to Python code that can be included into our script.

import base64
import os

def to_include(path):
    linelen = 74
    with open(path, 'rb') as img:
        data = base64.b85encode(img.read()).decode('ascii')
    i = 0
    print('# ' + path)
    print('data = base64.b85decode(')
    while i < len(data):
        print("    '" + data[i:i+linelen] + "'")
        i += linelen
    print(")" + os.linesep)

So we run:

to_include('python-powered-w-200x80-cmap.png')

The result is:

# python-powered-w-200x80-cmap.png
data = base64.b85decode(
    'iBL{Q4GJ0x0000DNk~Le0002U0000`2m=5B0Bf_#f&c&j1ZP1_K>z@;j|==^1poj5AY({UO#lF'
    'TCIA3{ga82g0001h=l}q9FaQARU;qF*m;eA5aGbhPJOBUy_E1bzMgRZ*%*@QYySrv)W{8N0?(Y'
    'A)%-xy0-H58pcdE>0nY&epyF_=YGk2<0h^l6YyLXwpcdEOYyUgzY|IFR)GiItYW|?M)s;ax)-R'
    '|z0nVCd)ncdy)h`Y>pnau9)?%mzpcXxNHs;ax)?yAh)-R}QHh`U6Hs{jAY|J_yp-Ddypng8y)|'
    'I9Q0|L*_HMF0QYGk3dYnauy)cmM8)|J{iH-9-QHs{ihHh`ZgYyUe@H?jlupcbTeYh?!N0nTVOY'
    'Rd<>H?#zg)yP2xH%-!9)%*<7pyZ_8pGgWspRfu<pszhdpGiHcHcZkf)-I=P)|NpzdM6#a%0004'
    'EOGiWihy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<wj5Z=>^HH1Yx4s&|TLb'
    'r%K%wE%hFsVhWK=95W|I+Y|Ejm;d3w>V;nL)-lud@?`O;dCUw0K84Y-3@;4K>6@7e$)#GZN$h`'
    'n$g7dZ63FZuKHTsQEnva+3%$t+N4H0&g?2;N6QnZbdH8QbHM3n~j>P5-j)IPvm{nc49d9EtxH<'
    'Y4l$tNY@4FC)7ooqs?@&u=qw|fp#1ZvTGBby=V}akZ=A_XU}T)Q-=-V+I^M-H}m_2wIW@zw4r4'
    '#YiNj<AgzW8u7%@0VL?POP@Cdiqr=@o{)yRU?#IH8Lp`Iuy5JjLlWq=44a;|s)tWfnZqhzjTDZ'
    '_Gb%2m=j|A!54*U2GcGAhVhzGvDlmlPuJ@`LEcXI-XawGbA4cS+$z7+m;dXg~xCcebdf|Kd5SN'
    'to}ZqiN*hrrVUA@8;#Ksx>8YARj#o|O8NEs{xpS0y#L;dmk%_In;ox7Q!;uF`mtAPndFdOS?QK'
    '!lFjhyG8QU!~N`?u%aZqNC`49-Llwt;ZUX0000bbVXQnWMOn=I%9HWVRU5xGB7bYEio`HFg8>%'
    'H99jmIxsXVFfckWFdV)gE&u=kC3HntbYx+4WjbwdWNBu305UK!GA%GSEiyM$GBG+ZHaapiD=;u'
    'RFfhRbg9`uv02y>eSaefwW^{L9a%BK_cXuvnZfkR6VQ^(GZ*pgw?mQX*0000<MNUMnLSTX'
)

This fragment can be included in a Python script. That script should also import base64. When the aforementioned script is run, data references the content of the original file.

In case this is compressed data you will have to add the decompression step yourself.


For comments, please send me an e-mail.


Related articles


←  ImageMagick: convert vs Wand How convenient is multithreading compared to multiprocessing?  →