]>
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.
31 -i Also output a function to return the image as a wxIcon.
37 import sys
, os
, glob
, getopt
, tempfile
, string
38 import cPickle
, cStringIO
, zlib
42 def crunch_data(data
, compressed
):
43 # convert the lines to a Python list, pickle it and optionally compress the result.
47 # the line is typically (but not always):
48 # [quote] <data> [quote][comma][newline]
50 # chop one char from the front
53 # now find the final quote and truncate there
54 quote
= string
.rfind(line
, "\"")
56 # and append the remaining data to our list
57 lines
.append(line
[:quote
])
60 # pickle, crunch and convert it to a form suitable for embedding in code
61 data
= cPickle
.dumps(lines
)
63 data
= zlib
.compress(data
, 9)
67 # This next bit is borrowed from PIL. It is used to wrap the text intelligently.
68 fp
= cStringIO
.StringIO()
69 data
= data
+ " " # buffer for the +1 test
72 octdigits
= "01234567"
73 hexdigits
= "0123456789abcdef"
79 if data
[i
+1] in octdigits
:
81 if data
[i
+n
] not in octdigits
:
85 elif data
[i
+1] == 'x':
87 if data
[i
+n
] not in hexdigits
:
102 # return the formatted compressed data
108 if not args
or ("-h" in args
):
119 opts
, fileArgs
= getopt
.getopt(args
, "auin:m:")
120 except getopt
.GetoptError
:
124 for opt
, val
in opts
:
136 if len(fileArgs
) != 2:
140 image_file
, python_file
= fileArgs
142 # convert the image file to a temporary file
143 tfname
= tempfile
.mktemp()
144 ok
, msg
= img2xpm
.convert(image_file
, maskClr
, None, tfname
)
149 data
= open(tfname
, "r").readlines()
150 data
= crunch_data(data
, compressed
)
154 out
= open(python_file
, "a")
156 out
= open(python_file
, "w")
158 out
.write("#" + "-" * 70 + "\n")
160 out
.write("# This file was generated by %s\n#\n" % sys
.argv
[0])
161 out
.write("from wxPython.wx import wxBitmapFromXPMData, wxImageFromBitmap\n")
163 out
.write("from wxPython.wx import wxIconFromXPMData\n")
165 out
.write("import cPickle, zlib\n\n\n")
167 out
.write("import cPickle\n\n\n")
170 out
.write("def get%sData():\n"
171 " return cPickle.loads(zlib.decompress(\n%s))\n\n"
174 out
.write("def get%sData():\n"
175 " return cPickle.loads(\n%s)\n\n"
179 out
.write("def get%sBitmap():\n"
180 " return wxBitmapFromXPMData(get%sData())\n\n"
181 "def get%sImage():\n"
182 " return wxImageFromBitmap(get%sBitmap())\n\n"
183 % tuple([imgName
] * 4))
185 out
.write("def get%sIcon():\n"
186 " return wxIconFromXPMData(get%sData())\n\n"
187 % tuple([imgName
] * 2))
191 n_msg
= ' using "%s"' % imgName
195 m_msg
= " with mask %s" % maskClr
198 print "Embedded %s%s into %s%s" % (image_file
, n_msg
, python_file
, m_msg
)
201 if __name__
== "__main__":