]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/tools/img2py.py
3 img2py.py -- Convert an image to XPM format and embed it in a Python
4 module with appropriate code so it can be loaded into
5 a program at runtime. The benefit is that since it is
6 Python source code it can be delivered as a .pyc or
7 'compiled' into the program using freeze, py2exe, etc.
11 img2py.py [options] image_file python_file
15 -m <#rrggbb> If the original image has a mask or transparency defined
16 it will be used by default. You can use this option to
17 override the default or provide a new mask by specifying
18 a colour in the image to mark as transparent.
20 -n <name> Normally generic names (getBitmap, etc.) are used for the
21 image access functions. If you use this option you can
22 specify a name that should be used to customize the access
23 fucntions, (getNameBitmap, etc.)
25 -a This flag specifies that the python_file should be appended
26 to instead of overwritten. This in combination with -n will
27 allow you to put multiple images in one Python source file.
29 -u Don't use compression. Leaves the data uncompressed.
35 import sys
, os
, glob
, getopt
, tempfile
36 import cPickle
, cStringIO
, zlib
40 def crunch_data(data
, compressed
):
41 # convert the lines to a Python list, pickle it and compress the result.
43 for line
in data
[2:]: # skip the first two lines
44 lines
.append(line
[1:-3]) # chop one char from the front and three from the end
46 # chop one extra char from the last line
47 lines
[-1] = lines
[-1][:-1]
49 # pickle, crunch and convert it to a form suitable for embedding in code
50 data
= cPickle
.dumps(lines
)
52 data
= zlib
.compress(data
, 9)
56 # This next bit is borrowed from PIL. It is used to wrap the text intelligently.
57 fp
= cStringIO
.StringIO()
58 data
= data
+ " " # buffer for the +1 test
61 octdigits
= "01234567"
67 if data
[i
+1] in octdigits
:
69 if data
[i
+n
] not in octdigits
:
83 # return the formatted compressed data
89 if not args
or ("-h" in args
):
99 opts
, fileArgs
= getopt
.getopt(args
, "aun:m:")
100 except getopt
.GetoptError
:
104 for opt
, val
in opts
:
114 if len(fileArgs
) != 2:
118 image_file
, python_file
= fileArgs
120 # convert the image file to a temporary file
121 tfname
= tempfile
.mktemp()
122 ok
, msg
= img2xpm
.convert(image_file
, maskClr
, None, tfname
)
127 data
= open(tfname
, "r").readlines()
128 data
= crunch_data(data
, compressed
)
132 out
= open(python_file
, "a")
134 out
= open(python_file
, "w")
136 out
.write("#" + "-" * 70 + "\n")
138 out
.write("# This file was generated by %s\n#\n" % sys
.argv
[0])
139 out
.write("from wxPython.wx import wxBitmapFromXPMData, wxImageFromBitmap\n")
141 out
.write("import cPickle, zlib\n\n\n")
143 out
.write("import cPickle\n\n\n")
146 out
.write("def get%sData():\n"
147 " return cPickle.loads(zlib.decompress(\n%s))\n\n"
150 out
.write("def get%sData():\n"
151 " return cPickle.loads(\n%s)\n\n"
155 out
.write("def get%sBitmap():\n"
156 " return wxBitmapFromXPMData(get%sData())\n\n"
157 "def get%sImage():\n"
158 " return wxImageFromBitmap(get%sBitmap())\n\n"
159 % tuple([imgName
] * 4))
162 n_msg
= ' using "%s"' % imgName
166 m_msg
= " with mask %s" % maskClr
169 print "Embedded %s%s into %s%s" % (image_file
, n_msg
, python_file
, m_msg
)
172 if __name__
== "__main__":