]>
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"
76 if data
[i
+1] in octdigits
:
78 if data
[i
+n
] not in octdigits
:
92 # return the formatted compressed data
98 if not args
or ("-h" in args
):
108 opts
, fileArgs
= getopt
.getopt(args
, "aun:m:")
109 except getopt
.GetoptError
:
113 for opt
, val
in opts
:
123 if len(fileArgs
) != 2:
127 image_file
, python_file
= fileArgs
129 # convert the image file to a temporary file
130 tfname
= tempfile
.mktemp()
131 ok
, msg
= img2xpm
.convert(image_file
, maskClr
, None, tfname
)
136 data
= open(tfname
, "r").readlines()
137 data
= crunch_data(data
, compressed
)
141 out
= open(python_file
, "a")
143 out
= open(python_file
, "w")
145 out
.write("#" + "-" * 70 + "\n")
147 out
.write("# This file was generated by %s\n#\n" % sys
.argv
[0])
148 out
.write("from wxPython.wx import wxBitmapFromXPMData, wxImageFromBitmap\n")
150 out
.write("import cPickle, zlib\n\n\n")
152 out
.write("import cPickle\n\n\n")
155 out
.write("def get%sData():\n"
156 " return cPickle.loads(zlib.decompress(\n%s))\n\n"
159 out
.write("def get%sData():\n"
160 " return cPickle.loads(\n%s)\n\n"
164 out
.write("def get%sBitmap():\n"
165 " return wxBitmapFromXPMData(get%sData())\n\n"
166 "def get%sImage():\n"
167 " return wxImageFromBitmap(get%sBitmap())\n\n"
168 % tuple([imgName
] * 4))
171 n_msg
= ' using "%s"' % imgName
175 m_msg
= " with mask %s" % maskClr
178 print "Embedded %s%s into %s%s" % (image_file
, n_msg
, python_file
, m_msg
)
181 if __name__
== "__main__":