]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/tools/img2py.py
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.
11 img2py.py [options] image_file python_file
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.
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.)
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.
29 -u Don't use compression. Leaves the data uncompressed.
31 -i Also output a function to return the image as a wxIcon.
37 import sys
, os
, glob
, getopt
, tempfile
, string
38 import cPickle
, cStringIO
, zlib
40 from wxPython
import wx
43 def crunch_data(data
, compressed
):
46 data
= zlib
.compress(data
, 9)
48 # convert to a printable format, so it can be in a Python source file
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
56 octdigits
= "01234567"
57 hexdigits
= "0123456789abcdef"
63 if data
[i
+1] in octdigits
:
65 if data
[i
+n
] not in octdigits
:
69 elif data
[i
+1] == 'x':
71 if data
[i
+n
] not in hexdigits
:
86 # return the formatted compressed data
92 if not args
or ("-h" in args
):
103 opts
, fileArgs
= getopt
.getopt(args
, "auin:m:")
104 except getopt
.GetoptError
:
108 for opt
, val
in opts
:
120 if len(fileArgs
) != 2:
124 image_file
, python_file
= fileArgs
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")
133 data
= open(tfname
, "rb").read()
134 data
= crunch_data(data
, compressed
)
138 out
= open(python_file
, "a")
140 out
= open(python_file
, "w")
142 out
.write("#" + "-" * 70 + "\n")
144 out
.write("# This file was generated by %s\n#\n" % sys
.argv
[0])
145 out
.write("from wxPython.wx import wxImageFromStream, wxBitmapFromImage\n")
147 out
.write("from wxPython.wx import wxEmptyIcon\n")
149 out
.write("import cStringIO, zlib\n\n\n")
151 out
.write("import cStringIO\n\n\n")
154 out
.write("def get%sData():\n"
155 " return zlib.decompress(\n%s)\n\n"
158 out
.write("def get%sData():\n"
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))
170 out
.write("def get%sIcon():\n"
171 " icon = wxEmptyIcon()\n"
172 " icon.CopyFromBitmap(get%sBitmap())\n"
174 % tuple([imgName
] * 2))
178 n_msg
= ' using "%s"' % imgName
182 m_msg
= " with mask %s" % maskClr
185 print "Embedded %s%s into %s%s" % (image_file
, n_msg
, python_file
, m_msg
)
188 if __name__
== "__main__":