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