]> git.saurik.com Git - wxWidgets.git/blob - wxPython/tools/img2xpm.py
Images can now be embedded in Python source files.
[wxWidgets.git] / wxPython / tools / img2xpm.py
1 #!/usr/bin/env python
2 """
3 img2xpm.py -- convert several image formats to XPM
4
5 Usage:
6
7 img2xpm.py [options] image_files...
8
9 Options:
10
11 -o <dir> The directory to place the .xmp file(s), defaults to
12 the current directory.
13
14 -m <#rrggbb> If the original image has a mask or transparency defined
15 it will be used by default. You can use this option to
16 override the default or provide a new mask by specifying
17 a colour in the image to mark as transparent.
18
19 -n <name> A filename to write the .xpm data to. Defaults to the
20 basename of the image file + '.xpm' This option overrides
21 the -o option.
22 """
23
24
25 import sys, os, glob, getopt, string
26 from wxPython.wx import *
27 wxInitAllImageHandlers()
28
29
30 def convert(file, maskClr, outputDir, outputName):
31 if string.lower(os.path.splitext(file)[1]) == ".ico":
32 icon = wxIcon(file, wxBITMAP_TYPE_ICO)
33 img = wxBitmapFromIcon(icon)
34 else:
35 img = wxBitmap(file, wxBITMAP_TYPE_ANY)
36
37 if not img.Ok():
38 return 0, file + " failed to load!"
39 else:
40 if maskClr:
41 om = img.GetMask()
42 mask = wxMaskColour(img, maskClr)
43 img.SetMask(mask)
44 if om is not None:
45 om.Destroy()
46 if outputName:
47 newname = outputName
48 else:
49 newname = os.path.join(outputDir, os.path.basename(os.path.splitext(file)[0]) + ".xpm")
50 if img.SaveFile(newname, wxBITMAP_TYPE_XPM):
51 return 1, file + " converted to " + newname
52 else:
53 return 0, file + " failed to save!"
54
55
56
57 def main(args):
58 if not args or ("-h" in args):
59 print __doc__
60 return
61
62 outputDir = ""
63 maskClr = None
64 outputName = None
65
66 try:
67 opts, fileArgs = getopt.getopt(args, "m:n:o:")
68 except getopt.GetoptError:
69 print __doc__
70 return
71
72 for opt, val in opts:
73 if opt == "-m":
74 maskClr = val
75 elif opt == "-n":
76 outputName = val
77 elif opt == "-o":
78 outputDir = val
79
80 if not fileArgs:
81 print __doc__
82 return
83
84 for arg in fileArgs:
85 for file in glob.glob(arg):
86 if not os.path.isfile(file):
87 continue
88 ok, msg = convert(file, maskClr, outputDir, outputName)
89 print msg
90
91
92
93 if __name__ == "__main__":
94 main(sys.argv[1:])
95
96