]>
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 #----------------------------------------------------------------------
11 # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
13 # o V2.5 compatability update
18 img2py.py -- Convert an image to PNG format and embed it in a Python
19 module with appropriate code so it can be loaded into
20 a program at runtime. The benefit is that since it is
21 Python source code it can be delivered as a .pyc or
22 'compiled' into the program using freeze, py2exe, etc.
26 img2py.py [options] image_file python_file
30 -m <#rrggbb> If the original image has a mask or transparency defined
31 it will be used by default. You can use this option to
32 override the default or provide a new mask by specifying
33 a colour in the image to mark as transparent.
35 -n <name> Normally generic names (getBitmap, etc.) are used for the
36 image access functions. If you use this option you can
37 specify a name that should be used to customize the access
38 fucntions, (getNameBitmap, etc.)
40 -c Maintain a catalog of names that can be used to reference
41 images. Catalog can be accessed via catalog and index attributes
42 of the module. If the -n <name> option is specified then <name>
43 is used for the catalog key and index value, otherwise
44 the filename without any path or extension is used as the key.
46 -a This flag specifies that the python_file should be appended
47 to instead of overwritten. This in combination with -n will
48 allow you to put multiple images in one Python source file.
50 -u Don't use compression. Leaves the data uncompressed.
52 -i Also output a function to return the image as a wxIcon.
58 # - Cliff Wells <LogiplexSoftware@earthlink.net>
59 # 20021206: Added catalog (-c) option.
61 # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
63 # o V2.5 compatability update
80 def crunch_data(data
, compressed
):
83 data
= zlib
.compress(data
, 9)
85 # convert to a printable format, so it can be in a Python source file
88 # This next bit is borrowed from PIL. It is used to wrap the text intelligently.
89 fp
= cStringIO
.StringIO()
90 data
= data
+ " " # buffer for the +1 test
93 octdigits
= "01234567"
94 hexdigits
= "0123456789abcdef"
100 if data
[i
+1] in octdigits
:
101 for n
in range(2, 5):
102 if data
[i
+n
] not in octdigits
:
106 elif data
[i
+1] == 'x':
107 for n
in range(2, 5):
108 if data
[i
+n
] not in hexdigits
:
123 # return the formatted compressed data
129 if not args
or ("-h" in args
):
133 # some bitmap related things need to have a wxApp initialized...
134 if wx
.GetApp() is None:
136 app
= wx
.PySimpleApp()
146 opts
, fileArgs
= getopt
.getopt(args
, "auicn:m:")
147 except getopt
.GetoptError
:
151 for opt
, val
in opts
:
165 if len(fileArgs
) != 2:
169 image_file
, python_file
= fileArgs
171 # convert the image file to a temporary file
172 tfname
= tempfile
.mktemp()
173 ok
, msg
= img2img
.convert(image_file
, maskClr
, None, tfname
, wx
.BITMAP_TYPE_PNG
, ".png")
178 data
= open(tfname
, "rb").read()
179 data
= crunch_data(data
, compressed
)
183 out
= open(python_file
, "a")
185 out
= open(python_file
, "w")
188 pyPath
, pyFile
= os
.path
.split(python_file
)
189 imgPath
, imgFile
= os
.path
.split(image_file
)
192 imgName
= os
.path
.splitext(imgFile
)[0]
193 print "\nWarning: -n not specified. Using filename (%s) for catalog entry." % imgName
197 # check to see if catalog exists already (file may have been created
198 # with an earlier version of img2py or without -c option)
199 oldSysPath
= sys
.path
[:]
200 sys
.path
= [pyPath
] # make sure we don't import something else by accident
201 mod
= __import__(os
.path
.splitext(pyFile
)[0])
202 if 'index' not in dir(mod
):
203 print "\nWarning: %s was originally created without catalog." % python_file
204 print " Any images already in file will not be cataloged.\n"
205 out
.write("\n# ***************** Catalog starts here *******************")
206 out
.write("\n\ncatalog = {}\n")
207 out
.write("index = []\n\n")
208 out
.write("class ImageClass: pass\n\n")
209 else: # save a copy of the old index so we can warn about duplicate names
210 old_index
[:] = mod
.index
[:]
212 sys
.path
= oldSysPath
[:]
214 out
.write("#" + "-" * 70 + "\n")
216 out
.write("# This file was generated by %s\n#\n" % sys
.argv
[0])
217 out
.write("from wx import ImageFromStream, BitmapFromImage\n")
219 out
.write("from wx import EmptyIcon\n")
221 out
.write("import cStringIO, zlib\n\n\n")
223 out
.write("import cStringIO\n\n\n")
226 out
.write("catalog = {}\n")
227 out
.write("index = []\n\n")
228 out
.write("class ImageClass: pass\n\n")
231 out
.write("def get%sData():\n"
232 " return zlib.decompress(\n%s)\n\n"
235 out
.write("def get%sData():\n"
240 out
.write("def get%sBitmap():\n"
241 " return BitmapFromImage(get%sImage())\n\n"
242 "def get%sImage():\n"
243 " stream = cStringIO.StringIO(get%sData())\n"
244 " return ImageFromStream(stream)\n\n"
245 % tuple([imgName
] * 4))
247 out
.write("def get%sIcon():\n"
248 " icon = EmptyIcon()\n"
249 " icon.CopyFromBitmap(get%sBitmap())\n"
251 % tuple([imgName
] * 2))
254 if imgName
in old_index
:
255 print "Warning: %s already in catalog." % imgName
256 print " Only the last entry will be accessible.\n"
257 old_index
.append(imgName
)
258 out
.write("index.append('%s')\n" % imgName
)
259 out
.write("catalog['%s'] = ImageClass()\n" % imgName
)
260 out
.write("catalog['%s'].getData = get%sData\n" % tuple([imgName
] * 2))
261 out
.write("catalog['%s'].getImage = get%sImage\n" % tuple([imgName
] * 2))
262 out
.write("catalog['%s'].getBitmap = get%sBitmap\n" % tuple([imgName
] * 2))
264 out
.write("catalog['%s'].getIcon = get%sIcon\n" % tuple([imgName
] * 2))
268 n_msg
= ' using "%s"' % imgName
272 m_msg
= " with mask %s" % maskClr
275 print "Embedded %s%s into %s%s" % (image_file
, n_msg
, python_file
, m_msg
)
278 if __name__
== "__main__":