Adjusting window level with Python commands

Are there Python commands that do the same things as the various click and drag options of the Adjust Window level button? If so what is the syntax of these Python commands?
Thanks,
Rohan Nadkarni

Hi there,
if you have your volume node nVol, you can use these two lines:

nVol.GetDisplayNode().SetAutoWindowLevel(False)
nVol.GetDisplayNode().SetWindowLevel(window,level)

where window and level are float values.

1 Like

you can also do

def setAndObserveColorNode(nVol, colorMap = "PET-Heat"):
    """
    set observable in color node
    """
    displayNode = nVol.GetScalarVolumeDisplayNode()
    if displayNode is not None:
        colornodeID = slicer.util.getFirstNodeByName(colorMap).GetID()
        displayNode.SetAndObserveColorNodeID(colornodeID)
        # Refresh Window Level
        displayNode.AutoWindowLevelOff()
        displayNode.AutoWindowLevelOn()

That way you control the color map and refresh window level

1 Like