]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/tools/img2img.py
3ce1e9f7ff2e6c163f1b8921a649fe6c57e2a3d3
[wxWidgets.git] / wxPython / wxPython / tools / img2img.py
1 """
2 Common routines for the image converter utilities.
3 """
4 import sys, os, glob, getopt, string
5 from wxPython.wx import *
6
7 if wxPlatform == "__WXGTK__":
8 # some bitmap related things need to have a wxApp initialized...
9 app = wxPySimpleApp()
10
11 wxInitAllImageHandlers()
12
13 def convert(file, maskClr, outputDir, outputName, outType, outExt):
14 if string.lower(os.path.splitext(file)[1]) == ".ico":
15 icon = wxIcon(file, wxBITMAP_TYPE_ICO)
16 img = wxBitmapFromIcon(icon)
17 else:
18 img = wxBitmap(file, wxBITMAP_TYPE_ANY)
19
20 if not img.Ok():
21 return 0, file + " failed to load!"
22 else:
23 if maskClr:
24 om = img.GetMask()
25 mask = wxMaskColour(img, maskClr)
26 img.SetMask(mask)
27 if om is not None:
28 om.Destroy()
29 if outputName:
30 newname = outputName
31 else:
32 newname = os.path.join(outputDir,
33 os.path.basename(os.path.splitext(file)[0]) + outExt)
34 if img.SaveFile(newname, outType):
35 return 1, file + " converted to " + newname
36 else:
37 img = wxImageFromBitmap(img)
38 if img.SaveFile(newname, outType):
39 return 1, "ok"
40 else:
41 return 0, file + " failed to save!"
42
43
44
45
46 def main(args, outType, outExt, doc):
47 if not args or ("-h" in args):
48 print doc
49 return
50
51 outputDir = ""
52 maskClr = None
53 outputName = None
54
55 try:
56 opts, fileArgs = getopt.getopt(args, "m:n:o:")
57 except getopt.GetoptError:
58 print __doc__
59 return
60
61 for opt, val in opts:
62 if opt == "-m":
63 maskClr = val
64 elif opt == "-n":
65 outputName = val
66 elif opt == "-o":
67 outputDir = val
68
69 if not fileArgs:
70 print doc
71 return
72
73 for arg in fileArgs:
74 for file in glob.glob(arg):
75 if not os.path.isfile(file):
76 continue
77 ok, msg = convert(file, maskClr, outputDir, outputName,
78 outType, outExt)
79 print msg
80