2003-11-12 16:34:20 -05:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
# Name: wxPython.tools.img2img
|
|
|
|
# Purpose: Common routines for the image converter utilities.
|
|
|
|
#
|
|
|
|
# Author: Robin Dunn
|
|
|
|
#
|
|
|
|
# RCS-ID: $Id$
|
|
|
|
# Copyright: (c) 2002 by Total Control Software
|
|
|
|
# Licence: wxWindows license
|
|
|
|
#----------------------------------------------------------------------
|
2003-12-22 14:09:54 -05:00
|
|
|
# 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
|
|
|
|
#
|
|
|
|
# o V2.5 compatability update
|
|
|
|
#
|
2003-07-02 19:13:10 -04:00
|
|
|
|
2003-12-22 14:09:54 -05:00
|
|
|
import getopt
|
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
import sys
|
2003-07-02 19:13:10 -04:00
|
|
|
|
2003-12-22 14:09:54 -05:00
|
|
|
import wx
|
2003-11-12 16:34:20 -05:00
|
|
|
|
|
|
|
def convert(file, maskClr, outputDir, outputName, outType, outExt):
|
|
|
|
if os.path.splitext(file)[1].lower() == ".ico":
|
2003-12-22 14:09:54 -05:00
|
|
|
icon = wx.Icon(file, wx.BITMAP_TYPE_ICO)
|
|
|
|
img = wx.BitmapFromIcon(icon)
|
2003-11-12 16:34:20 -05:00
|
|
|
else:
|
2003-12-22 14:09:54 -05:00
|
|
|
img = wx.Bitmap(file, wx.BITMAP_TYPE_ANY)
|
2003-11-12 16:34:20 -05:00
|
|
|
|
|
|
|
if not img.Ok():
|
|
|
|
return 0, file + " failed to load!"
|
|
|
|
else:
|
|
|
|
if maskClr:
|
|
|
|
om = img.GetMask()
|
2004-06-01 17:45:17 -04:00
|
|
|
mask = wx.Mask(img, maskClr)
|
2003-11-12 16:34:20 -05:00
|
|
|
img.SetMask(mask)
|
|
|
|
if om is not None:
|
|
|
|
om.Destroy()
|
|
|
|
if outputName:
|
|
|
|
newname = outputName
|
|
|
|
else:
|
|
|
|
newname = os.path.join(outputDir,
|
|
|
|
os.path.basename(os.path.splitext(file)[0]) + outExt)
|
|
|
|
if img.SaveFile(newname, outType):
|
|
|
|
return 1, file + " converted to " + newname
|
|
|
|
else:
|
2003-12-22 14:09:54 -05:00
|
|
|
img = wx.ImageFromBitmap(img)
|
2003-11-12 16:34:20 -05:00
|
|
|
if img.SaveFile(newname, outType):
|
|
|
|
return 1, "ok"
|
|
|
|
else:
|
|
|
|
return 0, file + " failed to save!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(args, outType, outExt, doc):
|
|
|
|
if not args or ("-h" in args):
|
|
|
|
print doc
|
|
|
|
return
|
|
|
|
|
|
|
|
outputDir = ""
|
|
|
|
maskClr = None
|
|
|
|
outputName = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
opts, fileArgs = getopt.getopt(args, "m:n:o:")
|
|
|
|
except getopt.GetoptError:
|
|
|
|
print __doc__
|
|
|
|
return
|
|
|
|
|
|
|
|
for opt, val in opts:
|
|
|
|
if opt == "-m":
|
|
|
|
maskClr = val
|
|
|
|
elif opt == "-n":
|
|
|
|
outputName = val
|
|
|
|
elif opt == "-o":
|
|
|
|
outputDir = val
|
|
|
|
|
|
|
|
if not fileArgs:
|
|
|
|
print doc
|
|
|
|
return
|
|
|
|
|
|
|
|
for arg in fileArgs:
|
|
|
|
for file in glob.glob(arg):
|
|
|
|
if not os.path.isfile(file):
|
|
|
|
continue
|
|
|
|
ok, msg = convert(file, maskClr, outputDir, outputName,
|
|
|
|
outType, outExt)
|
|
|
|
print msg
|
|
|
|
|