]> git.saurik.com Git - wxWidgets.git/blob - wxPython/tools/img2img.py
corrected assert even better
[wxWidgets.git] / 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 wxInitAllImageHandlers()
8
9 def convert(file, maskClr, outputDir, outputName, outType, outExt):
10 if string.lower(os.path.splitext(file)[1]) == ".ico":
11 icon = wxIcon(file, wxBITMAP_TYPE_ICO)
12 img = wxBitmapFromIcon(icon)
13 else:
14 img = wxBitmap(file, wxBITMAP_TYPE_ANY)
15
16 if not img.Ok():
17 return 0, file + " failed to load!"
18 else:
19 if maskClr:
20 om = img.GetMask()
21 mask = wxMaskColour(img, maskClr)
22 img.SetMask(mask)
23 if om is not None:
24 om.Destroy()
25 if outputName:
26 newname = outputName
27 else:
28 newname = os.path.join(outputDir,
29 os.path.basename(os.path.splitext(file)[0]) + outExt)
30 if img.SaveFile(newname, outType):
31 return 1, file + " converted to " + newname
32 else:
33 img = wxImageFromBitmap(img)
34 if img.SaveFile(newname, outType):
35 return 1, "ok"
36 else:
37 return 0, file + " failed to save!"
38
39
40
41
42 def main(args, outType, outExt, doc):
43 if not args or ("-h" in args):
44 print doc
45 return
46
47 outputDir = ""
48 maskClr = None
49 outputName = None
50
51 try:
52 opts, fileArgs = getopt.getopt(args, "m:n:o:")
53 except getopt.GetoptError:
54 print __doc__
55 return
56
57 for opt, val in opts:
58 if opt == "-m":
59 maskClr = val
60 elif opt == "-n":
61 outputName = val
62 elif opt == "-o":
63 outputDir = val
64
65 if not fileArgs:
66 print doc
67 return
68
69 for arg in fileArgs:
70 for file in glob.glob(arg):
71 if not os.path.isfile(file):
72 continue
73 ok, msg = convert(file, maskClr, outputDir, outputName,
74 outType, outExt)
75 print msg
76