]> git.saurik.com Git - wxWidgets.git/blame - wxPython/tools/img2py.py
Decided I need copy and assignment afterall
[wxWidgets.git] / wxPython / tools / img2py.py
CommitLineData
96bfd053
RD
1#!/usr/bin/env python
2"""
afb810d9 3img2py.py -- Convert an image to PNG format and embed it in a Python
96bfd053
RD
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
9Usage:
10
11 img2py.py [options] image_file python_file
12
13Options:
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
7fa0effc
RD
31 -i Also output a function to return the image as a wxIcon.
32
96bfd053
RD
33"""
34
35
36
7905ec55 37import sys, os, glob, getopt, tempfile, string
96bfd053 38import cPickle, cStringIO, zlib
afb810d9
RD
39import img2img
40from wxPython import wx
96bfd053
RD
41
42
43def crunch_data(data, compressed):
afb810d9 44 # compress it?
96bfd053
RD
45 if compressed:
46 data = zlib.compress(data, 9)
96bfd053 47
afb810d9
RD
48 # convert to a printable format, so it can be in a Python source file
49 data = repr(data)
96bfd053
RD
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"
286e2db6 57 hexdigits = "0123456789abcdef"
96bfd053
RD
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
286e2db6
RD
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
96bfd053
RD
75 else:
76 word = data[i:i+2]
77 i = i + 2
286e2db6 78
96bfd053
RD
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
91def 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 = ""
7fa0effc 100 icon = 0
96bfd053
RD
101
102 try:
7fa0effc 103 opts, fileArgs = getopt.getopt(args, "auin:m:")
96bfd053
RD
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
7fa0effc
RD
117 elif opt == "-i":
118 icon = 1
96bfd053
RD
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()
afb810d9 128 ok, msg = img2img.convert(image_file, maskClr, None, tfname, wx.wxBITMAP_TYPE_PNG, ".png")
96bfd053
RD
129 if not ok:
130 print msg
131 return
132
afb810d9 133 data = open(tfname, "rb").read()
96bfd053
RD
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])
afb810d9 145 out.write("from wxPython.wx import wxImageFromStream, wxBitmapFromImage\n")
7fa0effc 146 if icon:
afb810d9 147 out.write("from wxPython.wx import wxEmptyIcon\n")
96bfd053 148 if compressed:
afb810d9 149 out.write("import cStringIO, zlib\n\n\n")
96bfd053 150 else:
afb810d9 151 out.write("import cStringIO\n\n\n")
96bfd053
RD
152
153 if compressed:
154 out.write("def get%sData():\n"
afb810d9 155 " return zlib.decompress(\n%s)\n\n"
96bfd053
RD
156 % (imgName, data))
157 else:
158 out.write("def get%sData():\n"
afb810d9 159 " return %s\n\n"
96bfd053
RD
160 % (imgName, data))
161
162
163 out.write("def get%sBitmap():\n"
afb810d9 164 " return wxBitmapFromImage(get%sImage())\n\n"
96bfd053 165 "def get%sImage():\n"
afb810d9
RD
166 " stream = cStringIO.StringIO(get%sData())\n"
167 " return wxImageFromStream(stream)\n\n"
96bfd053 168 % tuple([imgName] * 4))
7fa0effc
RD
169 if icon:
170 out.write("def get%sIcon():\n"
afb810d9
RD
171 " icon = wxEmptyIcon()\n"
172 " icon.CopyFromBitmap(get%sBitmap())\n"
173 " return icon\n\n"
7fa0effc
RD
174 % tuple([imgName] * 2))
175
96bfd053
RD
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
188if __name__ == "__main__":
189 main(sys.argv[1:])
190