def imageOperation(image1, operation, image2=None):
    """
    Perform operations with ``vtkImageData`` objects.

    `image2` can be a constant value.

    Possible operations are: ``+``, ``-``, ``/``, ``1/x``, ``sin``, ``cos``, ``exp``, ``log``,
    ``abs``, ``**2``, ``sqrt``, ``min``, ``max``, ``atan``, ``atan2``, ``median``,
    ``mag``, ``dot``, ``gradient``, ``divergence``, ``laplacian``.

    .. hint:: |imageOperations| |imageOperations.py|_
    """
    op = operation.lower()

    if op in ["median"]:
        mf = vtk.vtkImageMedian3D()
        mf.SetInputData(image1)
        mf.Update()
        return mf.GetOutput()
    elif op in ["mag"]:
        mf = vtk.vtkImageMagnitude()
        mf.SetInputData(image1)
        mf.Update()
        return mf.GetOutput()
    elif op in ["dot", "dotproduct"]:
        mf = vtk.vtkImageDotProduct()
        mf.SetInput1Data(image1)
        mf.SetInput2Data(image2)
        mf.Update()
        return mf.GetOutput()
    elif op in ["grad", "gradient"]:
        mf = vtk.vtkImageGradient()
        mf.SetDimensionality(3)
        mf.SetInputData(image1)
        mf.Update()
        return mf.GetOutput()
    elif op in ["div", "divergence"]:
        mf = vtk.vtkImageDivergence()
        mf.SetInputData(image1)
        mf.Update()
        return mf.GetOutput()
    elif op in ["laplacian"]:
        mf = vtk.vtkImageLaplacian()
        mf.SetDimensionality(3)
        mf.SetInputData(image1)
        mf.Update()
        return mf.GetOutput()

    mat = vtk.vtkImageMathematics()
    mat.SetInput1Data(image1)
    K = None
    if image2:
        if isinstance(image2, vtk.vtkImageData):
            mat.SetInput2Data(image2)
        else:  # assume image2 is a constant value
            K = image2
            mat.SetConstantK(K)
            mat.SetConstantC(K)

    if op in ["+", "add", "plus"]:
        if K:
            mat.SetOperationToAddConstant()
        else:
            mat.SetOperationToAdd()

    elif op in ["-", "subtract", "minus"]:
        if K:
            mat.SetConstantC(-K)
            mat.SetOperationToAddConstant()
        else:
            mat.SetOperationToSubtract()

    elif op in ["*", "multiply", "times"]:
        if K:
            mat.SetOperationToMultiplyByK()
        else:
            mat.SetOperationToMultiply()

    elif op in ["/", "divide"]:
        if K:
            mat.SetConstantK(1.0 / K)
            mat.SetOperationToMultiplyByK()
        else:
            mat.SetOperationToDivide()

    elif op in ["1/x", "invert"]:
        mat.SetOperationToInvert()
    elif op in ["sin"]:
        mat.SetOperationToSin()
    elif op in ["cos"]:
        mat.SetOperationToCos()
    elif op in ["exp"]:
        mat.SetOperationToExp()
    elif op in ["log"]:
        mat.SetOperationToLog()
    elif op in ["abs"]:
        mat.SetOperationToAbsoluteValue()
    elif op in ["**2", "square"]:
        mat.SetOperationToSquare()
    elif op in ["sqrt", "sqr"]:
        mat.SetOperationToSquareRoot()
    elif op in ["min"]:
        mat.SetOperationToMin()
    elif op in ["max"]:
        mat.SetOperationToMax()
    elif op in ["atan"]:
        mat.SetOperationToATAN()
    elif op in ["atan2"]:
        mat.SetOperationToATAN2()
    else:
        vc.printc("~times Error in imageOperation: unknown operation", operation, c=1)
        exit()
    mat.Update()
    return mat.GetOutput()
def imageOperation ( image1 , operation , image2 = None ) : op = operation . lower ( ) if op in [ "median" ] : mf = vtk . vtkImageMedian3D ( ) mf . SetInputData ( image1 ) mf . Update ( ) return mf . GetOutput ( ) elif op in [ "mag" ] : mf = vtk . vtkImageMagnitude ( ) mf . SetInputData ( image1 ) mf . Update ( ) return mf . GetOutput ( ) elif op in [ "dot" , "dotproduct" ] : mf = vtk . vtkImageDotProduct ( ) mf . SetInput1Data ( image1 ) mf . SetInput2Data ( image2 ) mf . Update ( ) return mf . GetOutput ( ) elif op in [ "grad" , "gradient" ] : mf = vtk . vtkImageGradient ( ) mf . SetDimensionality ( 3 ) mf . SetInputData ( image1 ) mf . Update ( ) return mf . GetOutput ( ) elif op in [ "div" , "divergence" ] : mf = vtk . vtkImageDivergence ( ) mf . SetInputData ( image1 ) mf . Update ( ) return mf . GetOutput ( ) elif op in [ "laplacian" ] : mf = vtk . vtkImageLaplacian ( ) mf . SetDimensionality ( 3 ) mf . SetInputData ( image1 ) mf . Update ( ) return mf . GetOutput ( ) mat = vtk . vtkImageMathematics ( ) mat . SetInput1Data ( image1 ) K = None if image2 : if isinstance ( image2 , vtk . vtkImageData ) : mat . SetInput2Data ( image2 ) else : K = image2 mat . SetConstantK ( K ) mat . SetConstantC ( K ) if op in [ "+" , "add" , "plus" ] : if K : mat . SetOperationToAddConstant ( ) else : mat . SetOperationToAdd ( ) elif op in [ "-" , "subtract" , "minus" ] : if K : mat . SetConstantC ( - K ) mat . SetOperationToAddConstant ( ) else : mat . SetOperationToSubtract ( ) elif op in [ "*" , "multiply" , "times" ] : if K : mat . SetOperationToMultiplyByK ( ) else : mat . SetOperationToMultiply ( ) elif op in [ "/" , "divide" ] : if K : mat . SetConstantK ( 1.0 / K ) mat . SetOperationToMultiplyByK ( ) else : mat . SetOperationToDivide ( ) elif op in [ "1/x" , "invert" ] : mat . SetOperationToInvert ( ) elif op in [ "sin" ] : mat . SetOperationToSin ( ) elif op in [ "cos" ] : mat . SetOperationToCos ( ) elif op in [ "exp" ] : mat . SetOperationToExp ( ) elif op in [ "log" ] : mat . SetOperationToLog ( ) elif op in [ "abs" ] : mat . SetOperationToAbsoluteValue ( ) elif op in [ "**2" , "square" ] : mat . SetOperationToSquare ( ) elif op in [ "sqrt" , "sqr" ] : mat . SetOperationToSquareRoot ( ) elif op in [ "min" ] : mat . SetOperationToMin ( ) elif op in [ "max" ] : mat . SetOperationToMax ( ) elif op in [ "atan" ] : mat . SetOperationToATAN ( ) elif op in [ "atan2" ] : mat . SetOperationToATAN2 ( ) else : vc . printc ( "~times Error in imageOperation: unknown operation" , operation , c = 1 ) exit ( ) mat . Update ( ) return mat . GetOutput ( )
Perform operations with ``vtkImageData`` objects.

    `image2` can be a constant value.

    Possible operations are: ``+``, ``-``, ``/``, ``1/x``, ``sin``, ``cos``, ``exp``, ``log``,
    ``abs``, ``**2``, ``sqrt``, ``min``, ``max``, ``atan``, ``atan2``, ``median``,
    ``mag``, ``dot``, ``gradient``, ``divergence``, ``laplacian``.

    .. hint:: |imageOperations| |imageOperations.py|_