2001-04-09 15:36:36 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
2002-02-26 17:35:10 -05:00
|
|
|
img2py.py -- Convert an image to PNG format and embed it in a Python
|
2001-04-09 15:36:36 -04:00
|
|
|
module with appropriate code so it can be loaded into
|
|
|
|
a program at runtime. The benefit is that since it is
|
|
|
|
Python source code it can be delivered as a .pyc or
|
|
|
|
'compiled' into the program using freeze, py2exe, etc.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
img2py.py [options] image_file python_file
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-m <#rrggbb> If the original image has a mask or transparency defined
|
|
|
|
it will be used by default. You can use this option to
|
|
|
|
override the default or provide a new mask by specifying
|
|
|
|
a colour in the image to mark as transparent.
|
|
|
|
|
|
|
|
-n <name> Normally generic names (getBitmap, etc.) are used for the
|
|
|
|
image access functions. If you use this option you can
|
|
|
|
specify a name that should be used to customize the access
|
|
|
|
fucntions, (getNameBitmap, etc.)
|
|
|
|
|
|
|
|
-a This flag specifies that the python_file should be appended
|
|
|
|
to instead of overwritten. This in combination with -n will
|
|
|
|
allow you to put multiple images in one Python source file.
|
|
|
|
|
|
|
|
-u Don't use compression. Leaves the data uncompressed.
|
|
|
|
|
2002-01-28 18:08:47 -05:00
|
|
|
-i Also output a function to return the image as a wxIcon.
|
|
|
|
|
2001-04-09 15:36:36 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-04-26 20:16:02 -04:00
|
|
|
import sys, os, glob, getopt, tempfile, string
|
2001-04-09 15:36:36 -04:00
|
|
|
import cPickle, cStringIO, zlib
|
2002-02-26 17:35:10 -05:00
|
|
|
import img2img
|
|
|
|
from wxPython import wx
|
2001-04-09 15:36:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
def crunch_data(data, compressed):
|
2002-02-26 17:35:10 -05:00
|
|
|
# compress it?
|
2001-04-09 15:36:36 -04:00
|
|
|
if compressed:
|
|
|
|
data = zlib.compress(data, 9)
|
|
|
|
|
2002-02-26 17:35:10 -05:00
|
|
|
# convert to a printable format, so it can be in a Python source file
|
|
|
|
data = repr(data)
|
2001-04-09 15:36:36 -04:00
|
|
|
|
|
|
|
# This next bit is borrowed from PIL. It is used to wrap the text intelligently.
|
|
|
|
fp = cStringIO.StringIO()
|
|
|
|
data = data + " " # buffer for the +1 test
|
|
|
|
c = i = 0
|
|
|
|
word = ""
|
|
|
|
octdigits = "01234567"
|
2001-07-06 00:11:34 -04:00
|
|
|
hexdigits = "0123456789abcdef"
|
2001-04-09 15:36:36 -04:00
|
|
|
while i < len(data):
|
|
|
|
if data[i] != "\\":
|
|
|
|
word = data[i]
|
|
|
|
i = i + 1
|
|
|
|
else:
|
|
|
|
if data[i+1] in octdigits:
|
|
|
|
for n in range(2, 5):
|
|
|
|
if data[i+n] not in octdigits:
|
|
|
|
break
|
|
|
|
word = data[i:i+n]
|
|
|
|
i = i + n
|
2001-07-06 00:11:34 -04:00
|
|
|
elif data[i+1] == 'x':
|
|
|
|
for n in range(2, 5):
|
|
|
|
if data[i+n] not in hexdigits:
|
|
|
|
break
|
|
|
|
word = data[i:i+n]
|
|
|
|
i = i + n
|
2001-04-09 15:36:36 -04:00
|
|
|
else:
|
|
|
|
word = data[i:i+2]
|
|
|
|
i = i + 2
|
2001-07-06 00:11:34 -04:00
|
|
|
|
2001-04-09 15:36:36 -04:00
|
|
|
l = len(word)
|
|
|
|
if c + l >= 78-1:
|
|
|
|
fp.write("\\\n")
|
|
|
|
c = 0
|
|
|
|
fp.write(word)
|
|
|
|
c = c + l
|
|
|
|
|
|
|
|
# return the formatted compressed data
|
|
|
|
return fp.getvalue()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
if not args or ("-h" in args):
|
|
|
|
print __doc__
|
|
|
|
return
|
|
|
|
|
|
|
|
append = 0
|
|
|
|
compressed = 1
|
|
|
|
maskClr = None
|
|
|
|
imgName = ""
|
2002-01-28 18:08:47 -05:00
|
|
|
icon = 0
|
2001-04-09 15:36:36 -04:00
|
|
|
|
|
|
|
try:
|
2002-01-28 18:08:47 -05:00
|
|
|
opts, fileArgs = getopt.getopt(args, "auin:m:")
|
2001-04-09 15:36:36 -04:00
|
|
|
except getopt.GetoptError:
|
|
|
|
print __doc__
|
|
|
|
return
|
|
|
|
|
|
|
|
for opt, val in opts:
|
|
|
|
if opt == "-a":
|
|
|
|
append = 1
|
|
|
|
elif opt == "-u":
|
|
|
|
compressed = 0
|
|
|
|
elif opt == "-n":
|
|
|
|
imgName = val
|
|
|
|
elif opt == "-m":
|
|
|
|
maskClr = val
|
2002-01-28 18:08:47 -05:00
|
|
|
elif opt == "-i":
|
|
|
|
icon = 1
|
2001-04-09 15:36:36 -04:00
|
|
|
|
|
|
|
if len(fileArgs) != 2:
|
|
|
|
print __doc__
|
|
|
|
return
|
|
|
|
|
|
|
|
image_file, python_file = fileArgs
|
|
|
|
|
|
|
|
# convert the image file to a temporary file
|
|
|
|
tfname = tempfile.mktemp()
|
2002-02-26 17:35:10 -05:00
|
|
|
ok, msg = img2img.convert(image_file, maskClr, None, tfname, wx.wxBITMAP_TYPE_PNG, ".png")
|
2001-04-09 15:36:36 -04:00
|
|
|
if not ok:
|
|
|
|
print msg
|
|
|
|
return
|
|
|
|
|
2002-02-26 17:35:10 -05:00
|
|
|
data = open(tfname, "rb").read()
|
2001-04-09 15:36:36 -04:00
|
|
|
data = crunch_data(data, compressed)
|
|
|
|
os.unlink(tfname)
|
|
|
|
|
|
|
|
if append:
|
|
|
|
out = open(python_file, "a")
|
|
|
|
else:
|
|
|
|
out = open(python_file, "w")
|
|
|
|
|
|
|
|
out.write("#" + "-" * 70 + "\n")
|
|
|
|
if not append:
|
|
|
|
out.write("# This file was generated by %s\n#\n" % sys.argv[0])
|
2002-02-26 17:35:10 -05:00
|
|
|
out.write("from wxPython.wx import wxImageFromStream, wxBitmapFromImage\n")
|
2002-01-28 18:08:47 -05:00
|
|
|
if icon:
|
2002-02-26 17:35:10 -05:00
|
|
|
out.write("from wxPython.wx import wxEmptyIcon\n")
|
2001-04-09 15:36:36 -04:00
|
|
|
if compressed:
|
2002-02-26 17:35:10 -05:00
|
|
|
out.write("import cStringIO, zlib\n\n\n")
|
2001-04-09 15:36:36 -04:00
|
|
|
else:
|
2002-02-26 17:35:10 -05:00
|
|
|
out.write("import cStringIO\n\n\n")
|
2001-04-09 15:36:36 -04:00
|
|
|
|
|
|
|
if compressed:
|
|
|
|
out.write("def get%sData():\n"
|
2002-02-26 17:35:10 -05:00
|
|
|
" return zlib.decompress(\n%s)\n\n"
|
2001-04-09 15:36:36 -04:00
|
|
|
% (imgName, data))
|
|
|
|
else:
|
|
|
|
out.write("def get%sData():\n"
|
2002-02-26 17:35:10 -05:00
|
|
|
" return %s\n\n"
|
2001-04-09 15:36:36 -04:00
|
|
|
% (imgName, data))
|
|
|
|
|
|
|
|
|
|
|
|
out.write("def get%sBitmap():\n"
|
2002-02-26 17:35:10 -05:00
|
|
|
" return wxBitmapFromImage(get%sImage())\n\n"
|
2001-04-09 15:36:36 -04:00
|
|
|
"def get%sImage():\n"
|
2002-02-26 17:35:10 -05:00
|
|
|
" stream = cStringIO.StringIO(get%sData())\n"
|
|
|
|
" return wxImageFromStream(stream)\n\n"
|
2001-04-09 15:36:36 -04:00
|
|
|
% tuple([imgName] * 4))
|
2002-01-28 18:08:47 -05:00
|
|
|
if icon:
|
|
|
|
out.write("def get%sIcon():\n"
|
2002-02-26 17:35:10 -05:00
|
|
|
" icon = wxEmptyIcon()\n"
|
|
|
|
" icon.CopyFromBitmap(get%sBitmap())\n"
|
|
|
|
" return icon\n\n"
|
2002-01-28 18:08:47 -05:00
|
|
|
% tuple([imgName] * 2))
|
|
|
|
|
2001-04-09 15:36:36 -04:00
|
|
|
|
|
|
|
if imgName:
|
|
|
|
n_msg = ' using "%s"' % imgName
|
|
|
|
else:
|
|
|
|
n_msg = ""
|
|
|
|
if maskClr:
|
|
|
|
m_msg = " with mask %s" % maskClr
|
|
|
|
else:
|
|
|
|
m_msg = ""
|
|
|
|
print "Embedded %s%s into %s%s" % (image_file, n_msg, python_file, m_msg)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv[1:])
|
|
|
|
|