OpenCVとPythonと私 appendix A

#!/usr/bin/python

import cv
from opencv import highgui
import sys
import getopt

inputName  = None
outputName = None
debug = False

def main () :

        global inputName 
        global outputName
        global debug

        try :
                opts, args = getopt.getopt( sys.argv[1:], "hd", ["help","debug"] )
        except getopt.GetoptError, err :
                print str( err )
                usage()
                sys.exit(2)

        if len( args ) != 0 :
               
                debugout( "Argument 0 :" + args[0] )
                debugout( "Argument 0 :" + args[1] )

                outputName = args[0]
                inputName  = args[1]

        else :
                usage()
                sys.exit(0)

        for o, a in opts :
   
                if o in ( "-h", "--help" ) :
                        usage()
                        sys.exit(0)
                elif o in ( "-d", "--debug" ) :
                        debug = True
                else :
                        assert False, "unhandled option"

def debugout ( str ) :
        if debug == True :
                print str

def usage() :
        print "Usage: showImage.py [OPTION] FILE"
        print " "
        print " -h, --help   Display this message."
        print " -d, --debug  Output debug log."

if __name__ == "__main__" :

        main()

        debugout( "Input file name is  : " + inputName  )
        debugout( "Output file name is : " + outputName )

        before = cv.LoadImageM( inputName , cv.CV_LOAD_IMAGE_GRAYSCALE )

        debugout( "width : " + str( before.width  ) )
        debugout( "height: " + str( before.height ) )

        cv.SaveImage( outputName , before )