]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/tools/img2py.py
3 img2py.py -- Convert an image to XPM 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.
35 import sys
, os
, glob
, getopt
, tempfile
, string
36 import cPickle
, cStringIO
, zlib
40 def crunch_data(data
, compressed
):
41 # convert the lines to a Python list, pickle it and compress the result.
45 # the line is typically (but not always):
46 # [quote] <data> [quote][comma][newline]
48 # chop one char from the front
51 # now find the final quote and truncate there
52 quote
= string
.rfind(line
, "\"")
54 # and append the remaining data to our list
55 lines
.append(line
[:quote
])
58 # pickle, crunch and convert it to a form suitable for embedding in code
59 data
= cPickle
.dumps(lines
)
61 data
= zlib
.compress(data
, 9)
65 # This next bit is borrowed from PIL. It is used to wrap the text intelligently.
66 fp
= cStringIO
.StringIO()
67 data
= data
+ " " # buffer for the +1 test
70 octdigits
= "01234567"
71 hexdigits
= "0123456789abcdef"
77 if data
[i
+1] in octdigits
:
79 if data
[i
+n
] not in octdigits
:
83 elif data
[i
+1] == 'x':
85 if data
[i
+n
] not in hexdigits
:
100 # return the formatted compressed data
106 if not args
or ("-h" in args
):
116 opts
, fileArgs
= getopt
.getopt(args
, "aun:m:")
117 except getopt
.GetoptError
:
121 for opt
, val
in opts
:
131 if len(fileArgs
) != 2:
135 image_file
, python_file
= fileArgs
137 # convert the image file to a temporary file
138 tfname
= tempfile
.mktemp()
139 ok
, msg
= img2xpm
.convert(image_file
, maskClr
, None, tfname
)
144 data
= open(tfname
, "r").readlines()
145 data
= crunch_data(data
, compressed
)
149 out
= open(python_file
, "a")
151 out
= open(python_file
, "w")
153 out
.write("#" + "-" * 70 + "\n")
155 out
.write("# This file was generated by %s\n#\n" % sys
.argv
[0])
156 out
.write("from wxPython.wx import wxBitmapFromXPMData, wxImageFromBitmap\n")
158 out
.write("import cPickle, zlib\n\n\n")
160 out
.write("import cPickle\n\n\n")
163 out
.write("def get%sData():\n"
164 " return cPickle.loads(zlib.decompress(\n%s))\n\n"
167 out
.write("def get%sData():\n"
168 " return cPickle.loads(\n%s)\n\n"
172 out
.write("def get%sBitmap():\n"
173 " return wxBitmapFromXPMData(get%sData())\n\n"
174 "def get%sImage():\n"
175 " return wxImageFromBitmap(get%sBitmap())\n\n"
176 % tuple([imgName
] * 4))
179 n_msg
= ' using "%s"' % imgName
183 m_msg
= " with mask %s" % maskClr
186 print "Embedded %s%s into %s%s" % (image_file
, n_msg
, python_file
, m_msg
)
189 if __name__
== "__main__":