]> git.saurik.com Git - wxWidgets.git/blob - wxPython/tools/img2xpm.py
Some tweaks and cleanup.
[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 .xpm 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
28 wxInitAllImageHandlers()
29
30 if wxPlatform == "__WXGTK__":
31 app = wxPySimpleApp() # Blech! the GUI needs initialized before
32 # bitmaps can be created...
33
34
35 def convert(file, maskClr, outputDir, outputName):
36 if string.lower(os.path.splitext(file)[1]) == ".ico":
37 icon = wxIcon(file, wxBITMAP_TYPE_ICO)
38 img = wxBitmapFromIcon(icon)
39 else:
40 img = wxBitmap(file, wxBITMAP_TYPE_ANY)
41
42 if not img.Ok():
43 return 0, file + " failed to load!"
44 else:
45 if maskClr:
46 om = img.GetMask()
47 mask = wxMaskColour(img, maskClr)
48 img.SetMask(mask)
49 if om is not None:
50 om.Destroy()
51 if outputName:
52 newname = outputName
53 else:
54 newname = os.path.join(outputDir, os.path.basename(os.path.splitext(file)[0]) + ".xpm")
55 if img.SaveFile(newname, wxBITMAP_TYPE_XPM):
56 return 1, file + " converted to " + newname
57 else:
58 img = wxImageFromBitmap(img)
59 if img.SaveFile(newname, wxBITMAP_TYPE_XPM):
60 return 1, "ok"
61 else:
62 return 0, file + " failed to save!"
63
64
65
66 def main(args):
67 if not args or ("-h" in args):
68 print __doc__
69 return
70
71 outputDir = ""
72 maskClr = None
73 outputName = None
74
75 try:
76 opts, fileArgs = getopt.getopt(args, "m:n:o:")
77 except getopt.GetoptError:
78 print __doc__
79 return
80
81 for opt, val in opts:
82 if opt == "-m":
83 maskClr = val
84 elif opt == "-n":
85 outputName = val
86 elif opt == "-o":
87 outputDir = val
88
89 if not fileArgs:
90 print __doc__
91 return
92
93 for arg in fileArgs:
94 for file in glob.glob(arg):
95 if not os.path.isfile(file):
96 continue
97 ok, msg = convert(file, maskClr, outputDir, outputName)
98 print msg
99
100
101
102 if __name__ == "__main__":
103 main(sys.argv[1:])
104
105