]> git.saurik.com Git - wxWidgets.git/blob - wxPython/tools/img2py.py
Updated version to 2.3.2 so apps can test for differences in new merged source
[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 while i < len(data):
72 if data[i] != "\\":
73 word = data[i]
74 i = i + 1
75 else:
76 if data[i+1] in octdigits:
77 for n in range(2, 5):
78 if data[i+n] not in octdigits:
79 break
80 word = data[i:i+n]
81 i = i + n
82 else:
83 word = data[i:i+2]
84 i = i + 2
85 l = len(word)
86 if c + l >= 78-1:
87 fp.write("\\\n")
88 c = 0
89 fp.write(word)
90 c = c + l
91
92 # return the formatted compressed data
93 return fp.getvalue()
94
95
96
97 def main(args):
98 if not args or ("-h" in args):
99 print __doc__
100 return
101
102 append = 0
103 compressed = 1
104 maskClr = None
105 imgName = ""
106
107 try:
108 opts, fileArgs = getopt.getopt(args, "aun:m:")
109 except getopt.GetoptError:
110 print __doc__
111 return
112
113 for opt, val in opts:
114 if opt == "-a":
115 append = 1
116 elif opt == "-u":
117 compressed = 0
118 elif opt == "-n":
119 imgName = val
120 elif opt == "-m":
121 maskClr = val
122
123 if len(fileArgs) != 2:
124 print __doc__
125 return
126
127 image_file, python_file = fileArgs
128
129 # convert the image file to a temporary file
130 tfname = tempfile.mktemp()
131 ok, msg = img2xpm.convert(image_file, maskClr, None, tfname)
132 if not ok:
133 print msg
134 return
135
136 data = open(tfname, "r").readlines()
137 data = crunch_data(data, compressed)
138 os.unlink(tfname)
139
140 if append:
141 out = open(python_file, "a")
142 else:
143 out = open(python_file, "w")
144
145 out.write("#" + "-" * 70 + "\n")
146 if not append:
147 out.write("# This file was generated by %s\n#\n" % sys.argv[0])
148 out.write("from wxPython.wx import wxBitmapFromXPMData, wxImageFromBitmap\n")
149 if compressed:
150 out.write("import cPickle, zlib\n\n\n")
151 else:
152 out.write("import cPickle\n\n\n")
153
154 if compressed:
155 out.write("def get%sData():\n"
156 " return cPickle.loads(zlib.decompress(\n%s))\n\n"
157 % (imgName, data))
158 else:
159 out.write("def get%sData():\n"
160 " return cPickle.loads(\n%s)\n\n"
161 % (imgName, data))
162
163
164 out.write("def get%sBitmap():\n"
165 " return wxBitmapFromXPMData(get%sData())\n\n"
166 "def get%sImage():\n"
167 " return wxImageFromBitmap(get%sBitmap())\n\n"
168 % tuple([imgName] * 4))
169
170 if imgName:
171 n_msg = ' using "%s"' % imgName
172 else:
173 n_msg = ""
174 if maskClr:
175 m_msg = " with mask %s" % maskClr
176 else:
177 m_msg = ""
178 print "Embedded %s%s into %s%s" % (image_file, n_msg, python_file, m_msg)
179
180
181 if __name__ == "__main__":
182 main(sys.argv[1:])
183
184
185
186
187