#!usr/bin/python ######################################################### # Author: Kenneth Stojevich # # Program: This is an image compositor script for # # use with Shake's command line interface. It # # will first check that both images are of the # # correct size and scale them accordingly if they # # need it, and then composite them on each other # # and save them out into the final image. # # Date: 03/11/2006 # ######################################################### # Import the required modules needed for the script import getopt, sys, re, os, commands, rgbimg class ShakeComp: ################################################# # Constructor for our Shake Compositing # ################################################# def __init__(self): # Setup our foreground, background, and final image variables self.background = "" self.foreground = "" self.finalImage = "endResult.tif" # Setup our new foreground and background image variables self.endBKG = "endBackground.tif" self.endFG = "endForeground.tif" # Do we want to automatically display the end result? self.display = 0 # Do we want to have the final image in NTSC or Widescreen self.format = "NTSC" # Script name of our command results self.saveScript = "" ################################################# # Help file if the user doesn't supply any # # arguments, or gives the -h argument. # ################################################# def helpMe(self): print "Command options: [-h] [-d] [-t] [-b] [-f] [-o] [-s]" print " [--help] [--display] [--format] [--background] [--foreground] [--outImage] [--saveScript]" print "" print "USAGE: [-h] [--help] You are reading it right now" print " [-d] [--display] Display the image after saving the final output" print " [-t] [--format] Image format you wish to have the final image in (NTSC or Widescreen)" print " [-b] [--background] Background image we will composite onto (string)" print " [-l] [--foreground] Foreground image we will composite onto the background (string)" print " [-o] [--outimage] The final composited image (string)" print " [-s] [--saveScript] Name to save the final script to" print " NOTE!!!! -s or --saveScript is currently giving bad results in the final image do not use" print "" print 'Composite Scene: python Main.py -s -t NTSC -d -b myBackground.tif -f myForeground.tif -o myCompositedImage.tif' ################################################# # Run through the list of arguments that was # # passed to this function and set the according # # values given by the argument. # ################################################# def setupShake(self, arguments=[]): if (len(arguments) == 0): self.helpMe() sys.exit() try: opts, args = getopt.getopt(arguments, "hdt:b:f:o:s:", ["help", "display=", "format=", "background=", "foreground=", "outimage=", "saveScript="]) except getopt.GetoptError: self.helpMe() sys.exit() for o, a in opts: if (o == "-h" or o == "--help"): self.helpMe() sys.exit() elif (o == "-d" or o == "--display"): self.display = 1 elif (o == "-t" or o == "--format"): self.format = a print self.format elif (o == "-b" or o == "--background"): self.background = a elif (o == "-f" or o == "--foreground"): self.foreground = a elif (o == "-o" or o == "--outimage"): self.finalImage = a elif (o == "-s" or o == "--saveScript"): self.saveScript = a else: self.helpMe() ################################################# # Do the compositing of the images into the # # final image result. # # What this function also does is resize both # # images to NTSC format or widescreen format # # whichever the user gives in the initial # # arguments, NTSC is the default. # ################################################# def compositeImages(self): ################################################# # If there is no background or foreground image # # supplied we send them to the help me function.# ################################################# if (self.background == "" or self.foreground == ""): print "No foreground or background image supplied." self.helpMe() raise SystemExit ################################################# # Check which format the user wants to have the # # end image in, either NTSC or Widescren format.# # It then resizes both images to the correct # # size while retaining the image's proportions. # ################################################# if (self.format == "NTSC" or self.format == "ntsc"): resizeFG = "/usr/local/shake-v3.50.0308/bin/shake " + self.foreground + " -resize 720 486 -fo " + self.endFG print resizeFG execFG = commands.getoutput(resizeFG) print execFG resizeBKG = "/usr/local/shake-v3.50.0308/bin/shake " + self.background + " -resize 720 486 -fo " + self.endBKG print resizeBKG printMe2 = commands.getoutput(resizeBKG) print printMe2 elif (self.format == "Widescreen" or self.format == "widescreen"): resizeFG = "/usr/local/shake-v3.50.0308/bin/shake " + self.foreground + " -resize 1920 1200 -fo " + self.endFG print resizeFG execFG = commands.getoutput(resizeFG) print execFG resizeBKG = "/usr/local/shake-v3.50.0308/bin/shake " + self.background + " -resize 1920 1200 -fo " + self.endBKG print resizeBKG printMe2 = commands.getoutput(resizeBKG) print printMe2 else: print "Invalid display format supplied." self.helpMe() raise SystemExit ################################################# # If there is a script given we will save the # # script that we are executing using Shake's # # commandline version. # # CURRENTLY THIS DOES NOT WORK PROPERLY!!!!! # ################################################# if (self.saveScript == ""): compositeMe = "/usr/local/shake-v3.50.0308/bin/shake " + self.endBKG + " -under " + self.endFG + " -fo " + self.finalImage print compositeMe toBeComposited = commands.getoutput(compositeMe) print toBeComposited else: compositeMe = "/usr/local/shake-v3.50.0308/bin/shake " + self.endBKG + " -under " + self.endFG + " -fo " + self.finalImage + " -savescript " + self.saveScript print compositeMe toBeComposited = commands.getoutput(compositeMe) print toBeComposited ################################################# # Check to see if we want to display the end # # result, if so display it using Shake. # ################################################# if (self.display == 1): displayMe = "/usr/local/shake-v3.50.0308/bin/shake " + self.finalImage displayImage = commands.getoutput(displayMe) print displayImage