]> git.saurik.com Git - wxWidgets.git/blame - wxPython/tools/img2xpm.py
Added missing wxImage methods
[wxWidgets.git] / wxPython / tools / img2xpm.py
CommitLineData
96bfd053
RD
1#!/usr/bin/env python
2"""
3img2xpm.py -- convert several image formats to XPM
4
5Usage:
6
7 img2xpm.py [options] image_files...
8
9Options:
10
926bb76c 11 -o <dir> The directory to place the .xpm file(s), defaults to
96bfd053
RD
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
25import sys, os, glob, getopt, string
26from wxPython.wx import *
bddab5e9 27
96bfd053 28wxInitAllImageHandlers()
926bb76c 29
96bfd053
RD
30
31def convert(file, maskClr, outputDir, outputName):
32 if string.lower(os.path.splitext(file)[1]) == ".ico":
33 icon = wxIcon(file, wxBITMAP_TYPE_ICO)
34 img = wxBitmapFromIcon(icon)
35 else:
36 img = wxBitmap(file, wxBITMAP_TYPE_ANY)
37
38 if not img.Ok():
39 return 0, file + " failed to load!"
40 else:
41 if maskClr:
42 om = img.GetMask()
43 mask = wxMaskColour(img, maskClr)
44 img.SetMask(mask)
45 if om is not None:
46 om.Destroy()
47 if outputName:
48 newname = outputName
49 else:
50 newname = os.path.join(outputDir, os.path.basename(os.path.splitext(file)[0]) + ".xpm")
51 if img.SaveFile(newname, wxBITMAP_TYPE_XPM):
52 return 1, file + " converted to " + newname
53 else:
bddab5e9
RD
54 img = wxImageFromBitmap(img)
55 if img.SaveFile(newname, wxBITMAP_TYPE_XPM):
56 return 1, "ok"
57 else:
58 return 0, file + " failed to save!"
96bfd053
RD
59
60
61
62def main(args):
63 if not args or ("-h" in args):
64 print __doc__
65 return
66
67 outputDir = ""
68 maskClr = None
69 outputName = None
70
71 try:
72 opts, fileArgs = getopt.getopt(args, "m:n:o:")
73 except getopt.GetoptError:
74 print __doc__
75 return
76
77 for opt, val in opts:
78 if opt == "-m":
79 maskClr = val
80 elif opt == "-n":
81 outputName = val
82 elif opt == "-o":
83 outputDir = val
84
85 if not fileArgs:
86 print __doc__
87 return
88
89 for arg in fileArgs:
90 for file in glob.glob(arg):
91 if not os.path.isfile(file):
92 continue
93 ok, msg = convert(file, maskClr, outputDir, outputName)
94 print msg
95
96
97
98if __name__ == "__main__":
99 main(sys.argv[1:])
100
101