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