]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/img2py.py
1 #----------------------------------------------------------------------
2 # Name: wxPython.tools.img2py
3 # Purpose: Convert an image to Python code.
8 # Copyright: (c) 2002 by Total Control Software
9 # Licence: wxWindows license
10 #----------------------------------------------------------------------
14 img2py.py -- Convert an image to PNG format and embed it in a Python
15 module with appropriate code so it can be loaded into
16 a program at runtime. The benefit is that since it is
17 Python source code it can be delivered as a .pyc or
18 'compiled' into the program using freeze, py2exe, etc.
22 img2py.py [options] image_file python_file
26 -m <#rrggbb> If the original image has a mask or transparency defined
27 it will be used by default. You can use this option to
28 override the default or provide a new mask by specifying
29 a colour in the image to mark as transparent.
31 -n <name> Normally generic names (getBitmap, etc.) are used for the
32 image access functions. If you use this option you can
33 specify a name that should be used to customize the access
34 fucntions, (getNameBitmap, etc.)
36 -c Maintain a catalog of names that can be used to reference
37 images. Catalog can be accessed via catalog and index attributes
38 of the module. If the -n <name> option is specified then <name>
39 is used for the catalog key and index value, otherwise
40 the filename without any path or extension is used as the key.
42 -a This flag specifies that the python_file should be appended
43 to instead of overwritten. This in combination with -n will
44 allow you to put multiple images in one Python source file.
46 -u Don't use compression. Leaves the data uncompressed.
48 -i Also output a function to return the image as a wxIcon.
54 # - Cliff Wells <LogiplexSoftware@earthlink.net>
55 # 20021206: Added catalog (-c) option.
59 import sys
, os
, glob
, getopt
, tempfile
60 import cPickle
, cStringIO
, zlib
62 from wxPython
import wx
65 def crunch_data(data
, compressed
):
68 data
= zlib
.compress(data
, 9)
70 # convert to a printable format, so it can be in a Python source file
73 # This next bit is borrowed from PIL. It is used to wrap the text intelligently.
74 fp
= cStringIO
.StringIO()
75 data
= data
+ " " # buffer for the +1 test
78 octdigits
= "01234567"
79 hexdigits
= "0123456789abcdef"
85 if data
[i
+1] in octdigits
:
87 if data
[i
+n
] not in octdigits
:
91 elif data
[i
+1] == 'x':
93 if data
[i
+n
] not in hexdigits
:
108 # return the formatted compressed data
114 if not args
or ("-h" in args
):
118 # some bitmap related things need to have a wxApp initialized...
119 if wx
.wxGetApp() is None:
120 app
= wx
.wxPySimpleApp()
130 opts
, fileArgs
= getopt
.getopt(args
, "auicn:m:")
131 except getopt
.GetoptError
:
135 for opt
, val
in opts
:
149 if len(fileArgs
) != 2:
153 image_file
, python_file
= fileArgs
155 # convert the image file to a temporary file
156 tfname
= tempfile
.mktemp()
157 ok
, msg
= img2img
.convert(image_file
, maskClr
, None, tfname
, wx
.wxBITMAP_TYPE_PNG
, ".png")
162 data
= open(tfname
, "rb").read()
163 data
= crunch_data(data
, compressed
)
167 out
= open(python_file
, "a")
169 out
= open(python_file
, "w")
172 pyPath
, pyFile
= os
.path
.split(python_file
)
173 imgPath
, imgFile
= os
.path
.split(image_file
)
176 imgName
= os
.path
.splitext(imgFile
)[0]
177 print "\nWarning: -n not specified. Using filename (%s) for catalog entry." % imgName
181 # check to see if catalog exists already (file may have been created
182 # with an earlier version of img2py or without -c option)
183 oldSysPath
= sys
.path
[:]
184 sys
.path
= [pyPath
] # make sure we don't import something else by accident
185 mod
= __import__(os
.path
.splitext(pyFile
)[0])
186 if 'index' not in dir(mod
):
187 print "\nWarning: %s was originally created without catalog." % python_file
188 print " Any images already in file will not be cataloged.\n"
189 out
.write("\n# ***************** Catalog starts here *******************")
190 out
.write("\n\ncatalog = {}\n")
191 out
.write("index = []\n\n")
192 out
.write("class ImageClass: pass\n\n")
193 else: # save a copy of the old index so we can warn about duplicate names
194 old_index
[:] = mod
.index
[:]
196 sys
.path
= oldSysPath
[:]
198 out
.write("#" + "-" * 70 + "\n")
200 out
.write("# This file was generated by %s\n#\n" % sys
.argv
[0])
201 out
.write("from wxPython.wx import wxImageFromStream, wxBitmapFromImage\n")
203 out
.write("from wxPython.wx import wxEmptyIcon\n")
205 out
.write("import cStringIO, zlib\n\n\n")
207 out
.write("import cStringIO\n\n\n")
210 out
.write("catalog = {}\n")
211 out
.write("index = []\n\n")
212 out
.write("class ImageClass: pass\n\n")
215 out
.write("def get%sData():\n"
216 " return zlib.decompress(\n%s)\n\n"
219 out
.write("def get%sData():\n"
224 out
.write("def get%sBitmap():\n"
225 " return wxBitmapFromImage(get%sImage())\n\n"
226 "def get%sImage():\n"
227 " stream = cStringIO.StringIO(get%sData())\n"
228 " return wxImageFromStream(stream)\n\n"
229 % tuple([imgName
] * 4))
231 out
.write("def get%sIcon():\n"
232 " icon = wxEmptyIcon()\n"
233 " icon.CopyFromBitmap(get%sBitmap())\n"
235 % tuple([imgName
] * 2))
238 if imgName
in old_index
:
239 print "Warning: %s already in catalog." % imgName
240 print " Only the last entry will be accessible.\n"
241 old_index
.append(imgName
)
242 out
.write("index.append('%s')\n" % imgName
)
243 out
.write("catalog['%s'] = ImageClass()\n" % imgName
)
244 out
.write("catalog['%s'].getData = get%sData\n" % tuple([imgName
] * 2))
245 out
.write("catalog['%s'].getImage = get%sImage\n" % tuple([imgName
] * 2))
246 out
.write("catalog['%s'].getBitmap = get%sBitmap\n" % tuple([imgName
] * 2))
248 out
.write("catalog['%s'].getIcon = get%sIcon\n" % tuple([imgName
] * 2))
252 n_msg
= ' using "%s"' % imgName
256 m_msg
= " with mask %s" % maskClr
259 print "Embedded %s%s into %s%s" % (image_file
, n_msg
, python_file
, m_msg
)
262 if __name__
== "__main__":