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