]> git.saurik.com Git - wxWidgets.git/blob - wxPython/tools/img2py.py
Committing in .
[wxWidgets.git] / wxPython / tools / img2py.py
1 #!/usr/bin/env python
2 """
3 img2py.py -- Convert an image to PNG 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 -i Also output a function to return the image as a wxIcon.
32
33 """
34
35
36
37 import sys, os, glob, getopt, tempfile, string
38 import cPickle, cStringIO, zlib
39 import img2img
40 from wxPython import wx
41
42
43 def crunch_data(data, compressed):
44 # compress it?
45 if compressed:
46 data = zlib.compress(data, 9)
47
48 # convert to a printable format, so it can be in a Python source file
49 data = repr(data)
50
51 # This next bit is borrowed from PIL. It is used to wrap the text intelligently.
52 fp = cStringIO.StringIO()
53 data = data + " " # buffer for the +1 test
54 c = i = 0
55 word = ""
56 octdigits = "01234567"
57 hexdigits = "0123456789abcdef"
58 while i < len(data):
59 if data[i] != "\\":
60 word = data[i]
61 i = i + 1
62 else:
63 if data[i+1] in octdigits:
64 for n in range(2, 5):
65 if data[i+n] not in octdigits:
66 break
67 word = data[i:i+n]
68 i = i + n
69 elif data[i+1] == 'x':
70 for n in range(2, 5):
71 if data[i+n] not in hexdigits:
72 break
73 word = data[i:i+n]
74 i = i + n
75 else:
76 word = data[i:i+2]
77 i = i + 2
78
79 l = len(word)
80 if c + l >= 78-1:
81 fp.write("\\\n")
82 c = 0
83 fp.write(word)
84 c = c + l
85
86 # return the formatted compressed data
87 return fp.getvalue()
88
89
90
91 def main(args):
92 if not args or ("-h" in args):
93 print __doc__
94 return
95
96 append = 0
97 compressed = 1
98 maskClr = None
99 imgName = ""
100 icon = 0
101
102 try:
103 opts, fileArgs = getopt.getopt(args, "auin:m:")
104 except getopt.GetoptError:
105 print __doc__
106 return
107
108 for opt, val in opts:
109 if opt == "-a":
110 append = 1
111 elif opt == "-u":
112 compressed = 0
113 elif opt == "-n":
114 imgName = val
115 elif opt == "-m":
116 maskClr = val
117 elif opt == "-i":
118 icon = 1
119
120 if len(fileArgs) != 2:
121 print __doc__
122 return
123
124 image_file, python_file = fileArgs
125
126 # convert the image file to a temporary file
127 tfname = tempfile.mktemp()
128 ok, msg = img2img.convert(image_file, maskClr, None, tfname, wx.wxBITMAP_TYPE_PNG, ".png")
129 if not ok:
130 print msg
131 return
132
133 data = open(tfname, "rb").read()
134 data = crunch_data(data, compressed)
135 os.unlink(tfname)
136
137 if append:
138 out = open(python_file, "a")
139 else:
140 out = open(python_file, "w")
141
142 out.write("#" + "-" * 70 + "\n")
143 if not append:
144 out.write("# This file was generated by %s\n#\n" % sys.argv[0])
145 out.write("from wxPython.wx import wxImageFromStream, wxBitmapFromImage\n")
146 if icon:
147 out.write("from wxPython.wx import wxEmptyIcon\n")
148 if compressed:
149 out.write("import cStringIO, zlib\n\n\n")
150 else:
151 out.write("import cStringIO\n\n\n")
152
153 if compressed:
154 out.write("def get%sData():\n"
155 " return zlib.decompress(\n%s)\n\n"
156 % (imgName, data))
157 else:
158 out.write("def get%sData():\n"
159 " return %s\n\n"
160 % (imgName, data))
161
162
163 out.write("def get%sBitmap():\n"
164 " return wxBitmapFromImage(get%sImage())\n\n"
165 "def get%sImage():\n"
166 " stream = cStringIO.StringIO(get%sData())\n"
167 " return wxImageFromStream(stream)\n\n"
168 % tuple([imgName] * 4))
169 if icon:
170 out.write("def get%sIcon():\n"
171 " icon = wxEmptyIcon()\n"
172 " icon.CopyFromBitmap(get%sBitmap())\n"
173 " return icon\n\n"
174 % tuple([imgName] * 2))
175
176
177 if imgName:
178 n_msg = ' using "%s"' % imgName
179 else:
180 n_msg = ""
181 if maskClr:
182 m_msg = " with mask %s" % maskClr
183 else:
184 m_msg = ""
185 print "Embedded %s%s into %s%s" % (image_file, n_msg, python_file, m_msg)
186
187
188 if __name__ == "__main__":
189 main(sys.argv[1:])
190