]> git.saurik.com Git - wxWidgets.git/blob - wxPython/tools/img2py.py
Compile fix.
[wxWidgets.git] / wxPython / tools / img2py.py
1 #!/usr/bin/env python
2 """
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.
8
9 Usage:
10
11 img2py.py [options] image_file python_file
12
13 Options:
14
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.
19
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.)
24
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.
28
29 -u Don't use compression. Leaves the data uncompressed.
30
31 """
32
33
34
35 import sys, os, glob, getopt, tempfile, string
36 import cPickle, cStringIO, zlib
37 import img2xpm
38
39
40 def crunch_data(data, compressed):
41 # convert the lines to a Python list, pickle it and compress the result.
42 lines = []
43 for line in data:
44 if line[0] == "\"":
45 # the line is typically (but not always):
46 # [quote] <data> [quote][comma][newline]
47
48 # chop one char from the front
49 line = line[1:]
50
51 # now find the final quote and truncate there
52 quote = string.rfind(line, "\"")
53
54 # and append the remaining data to our list
55 lines.append(line[:quote])
56
57
58 # pickle, crunch and convert it to a form suitable for embedding in code
59 data = cPickle.dumps(lines)
60 if compressed:
61 data = zlib.compress(data, 9)
62 data = repr(data)
63
64
65 # This next bit is borrowed from PIL. It is used to wrap the text intelligently.
66 fp = cStringIO.StringIO()
67 data = data + " " # buffer for the +1 test
68 c = i = 0
69 word = ""
70 octdigits = "01234567"
71 hexdigits = "0123456789abcdef"
72 while i < len(data):
73 if data[i] != "\\":
74 word = data[i]
75 i = i + 1
76 else:
77 if data[i+1] in octdigits:
78 for n in range(2, 5):
79 if data[i+n] not in octdigits:
80 break
81 word = data[i:i+n]
82 i = i + n
83 elif data[i+1] == 'x':
84 for n in range(2, 5):
85 if data[i+n] not in hexdigits:
86 break
87 word = data[i:i+n]
88 i = i + n
89 else:
90 word = data[i:i+2]
91 i = i + 2
92
93 l = len(word)
94 if c + l >= 78-1:
95 fp.write("\\\n")
96 c = 0
97 fp.write(word)
98 c = c + l
99
100 # return the formatted compressed data
101 return fp.getvalue()
102
103
104
105 def main(args):
106 if not args or ("-h" in args):
107 print __doc__
108 return
109
110 append = 0
111 compressed = 1
112 maskClr = None
113 imgName = ""
114
115 try:
116 opts, fileArgs = getopt.getopt(args, "aun:m:")
117 except getopt.GetoptError:
118 print __doc__
119 return
120
121 for opt, val in opts:
122 if opt == "-a":
123 append = 1
124 elif opt == "-u":
125 compressed = 0
126 elif opt == "-n":
127 imgName = val
128 elif opt == "-m":
129 maskClr = val
130
131 if len(fileArgs) != 2:
132 print __doc__
133 return
134
135 image_file, python_file = fileArgs
136
137 # convert the image file to a temporary file
138 tfname = tempfile.mktemp()
139 ok, msg = img2xpm.convert(image_file, maskClr, None, tfname)
140 if not ok:
141 print msg
142 return
143
144 data = open(tfname, "r").readlines()
145 data = crunch_data(data, compressed)
146 os.unlink(tfname)
147
148 if append:
149 out = open(python_file, "a")
150 else:
151 out = open(python_file, "w")
152
153 out.write("#" + "-" * 70 + "\n")
154 if not append:
155 out.write("# This file was generated by %s\n#\n" % sys.argv[0])
156 out.write("from wxPython.wx import wxBitmapFromXPMData, wxImageFromBitmap\n")
157 if compressed:
158 out.write("import cPickle, zlib\n\n\n")
159 else:
160 out.write("import cPickle\n\n\n")
161
162 if compressed:
163 out.write("def get%sData():\n"
164 " return cPickle.loads(zlib.decompress(\n%s))\n\n"
165 % (imgName, data))
166 else:
167 out.write("def get%sData():\n"
168 " return cPickle.loads(\n%s)\n\n"
169 % (imgName, data))
170
171
172 out.write("def get%sBitmap():\n"
173 " return wxBitmapFromXPMData(get%sData())\n\n"
174 "def get%sImage():\n"
175 " return wxImageFromBitmap(get%sBitmap())\n\n"
176 % tuple([imgName] * 4))
177
178 if imgName:
179 n_msg = ' using "%s"' % imgName
180 else:
181 n_msg = ""
182 if maskClr:
183 m_msg = " with mask %s" % maskClr
184 else:
185 m_msg = ""
186 print "Embedded %s%s into %s%s" % (image_file, n_msg, python_file, m_msg)
187
188
189 if __name__ == "__main__":
190 main(sys.argv[1:])
191
192
193
194
195