]> git.saurik.com Git - wxWidgets.git/blob - wxPython/tools/img2py.py
Images can now be embedded in Python source files.
[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
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[2:]: # skip the first two lines
44 lines.append(line[1:-3]) # chop one char from the front and three from the end
45
46 # chop one extra char from the last line
47 lines[-1] = lines[-1][:-1]
48
49 # pickle, crunch and convert it to a form suitable for embedding in code
50 data = cPickle.dumps(lines)
51 if compressed:
52 data = zlib.compress(data, 9)
53 data = repr(data)
54
55
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
59 c = i = 0
60 word = ""
61 octdigits = "01234567"
62 while i < len(data):
63 if data[i] != "\\":
64 word = data[i]
65 i = i + 1
66 else:
67 if data[i+1] in octdigits:
68 for n in range(2, 5):
69 if data[i+n] not in octdigits:
70 break
71 word = data[i:i+n]
72 i = i + n
73 else:
74 word = data[i:i+2]
75 i = i + 2
76 l = len(word)
77 if c + l >= 78-1:
78 fp.write("\\\n")
79 c = 0
80 fp.write(word)
81 c = c + l
82
83 # return the formatted compressed data
84 return fp.getvalue()
85
86
87
88 def main(args):
89 if not args or ("-h" in args):
90 print __doc__
91 return
92
93 append = 0
94 compressed = 1
95 maskClr = None
96 imgName = ""
97
98 try:
99 opts, fileArgs = getopt.getopt(args, "aun:m:")
100 except getopt.GetoptError:
101 print __doc__
102 return
103
104 for opt, val in opts:
105 if opt == "-a":
106 append = 1
107 elif opt == "-u":
108 compressed = 0
109 elif opt == "-n":
110 imgName = val
111 elif opt == "-m":
112 maskClr = val
113
114 if len(fileArgs) != 2:
115 print __doc__
116 return
117
118 image_file, python_file = fileArgs
119
120 # convert the image file to a temporary file
121 tfname = tempfile.mktemp()
122 ok, msg = img2xpm.convert(image_file, maskClr, None, tfname)
123 if not ok:
124 print msg
125 return
126
127 data = open(tfname, "r").readlines()
128 data = crunch_data(data, compressed)
129 os.unlink(tfname)
130
131 if append:
132 out = open(python_file, "a")
133 else:
134 out = open(python_file, "w")
135
136 out.write("#" + "-" * 70 + "\n")
137 if not append:
138 out.write("# This file was generated by %s\n#\n" % sys.argv[0])
139 out.write("from wxPython.wx import wxBitmapFromXPMData, wxImageFromBitmap\n")
140 if compressed:
141 out.write("import cPickle, zlib\n\n\n")
142 else:
143 out.write("import cPickle\n\n\n")
144
145 if compressed:
146 out.write("def get%sData():\n"
147 " return cPickle.loads(zlib.decompress(\n%s))\n\n"
148 % (imgName, data))
149 else:
150 out.write("def get%sData():\n"
151 " return cPickle.loads(\n%s)\n\n"
152 % (imgName, data))
153
154
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))
160
161 if imgName:
162 n_msg = ' using "%s"' % imgName
163 else:
164 n_msg = ""
165 if maskClr:
166 m_msg = " with mask %s" % maskClr
167 else:
168 m_msg = ""
169 print "Embedded %s%s into %s%s" % (image_file, n_msg, python_file, m_msg)
170
171
172 if __name__ == "__main__":
173 main(sys.argv[1:])
174
175
176
177
178