1.3.4 Using the Python interface
The applications can also be accessed from Python, through a module named otbApplication
On Unix systems it is typically available in the /usr/lib/otb/python directory. You may need to
configure the environment variable PYTHONPATH to include this directory so that the module becomes
available from an Python shell.
On Windows, you can install the otb-python package, and the module will be available from an
OSGeo4W shell automatically.
In this module, two main classes can be manipulated :
- Registry, which provides access to the list of available applications, and can create
applications
- Application, the base class for all applications. This allows to interact with an application
instance created by the Registry
As for the command line and GUI launchers, the path to the application modules needs to be properly set
with the OTB_APPLICATION_PATH environment variable. The standard location on Unix systems is
/usr/lib/otb/applications. On Windows, the applications are available in the otb-bin
OSGeo4W package, and the environment is configured automatically so you don’t need to tweak
OTB_APPLICATION_PATH.
Here is one example of how to use Python to run the Smoothing application, changing the algorithm at
each iteration.
# Example on the use of the Smoothing application # # We will use sys.argv to retrieve arguments from the command line. # Here, the script will accept an image file as first argument, # and the basename of the output files, without extension. from sys import argv # The python module providing access to OTB applications is otbApplication import otbApplication # otbApplication.Registry can tell you what application are available print "Available applications : " print str( otbApplication.Registry.GetAvailableApplications() ) # Let's create the application with codename "Smoothing" app = otbApplication.Registry.CreateApplication("Smoothing") # We print the keys of all its parameter print app.GetParametersKeys() # First, we set the input image filename app.SetParameterString("in", argv[1]) # The smoothing algorithm can be set with the "type" parameter key # and can take 3 values : 'mean', 'gaussian', 'anidif' for type in ['mean', 'gaussian', 'anidif']: print 'Running with ' + type + ' smoothing type' # Here we configure the smoothing algorithm app.SetParameterString("type", type) # Set the output filename, using the algorithm to differenciate the outputs app.SetParameterString("out", argv[2] + type + ".tif") # This will execute the application and save the output file app.ExecuteAndWriteOutput()