How to script into code data loading, 3D model generation and screen capture tasks

I’d like to be able to automate some tasks in 3D Slicer by calling its Python API from the command line:

  • Loading structural (e.g. an T1w MRI) and segmentation files for a given subject.
  • Making the data displayed at a given orientation/view (e.g. axial) at a specified slice/location/position (index).
  • Potentially creating a 3D model/contour out of a segmentation, and display it in an orthogonal view
  • Removing the default annotation labels (without requiring to restart 3D Slicer).
  • Capturing a screenshot of a view (e.g. axial) at an arbitrary size and resolution.
  • Setting the background of the view (whether a 3D or e.g. axial) to white or transparent (or black in the case of the 3D view).

So far I have found a way to capture the 3D or the orthogonal views using Python:

view =
renderWindow = view.renderWindow()
wti = vtk.vtkWindowToImageFilter()
vtk_data = wti.GetOutput()

writer = vtk.vtkPNGWriter()



filename = "/my_path/slicer_screenshot.png"

However, the above

# Disable slice annotations immediately
sliceAnnotations = slicer.modules.DataProbeInstance.infoWidget.sliceAnnotations
# Disable slice annotations persistently (after Slicer restarts)
settings = qt.QSettings()
settings.setValue("DataProbe/sliceViewAnnotations.enabled", 0)
  • The size of the captured screenshot has the size of the view/window (i.e it is a small window if I am in a 3D+orthogonal layout, a bigger one if I am on a single view layout, etc.). Can this be made arbitrarily big programmatically? If not, can the layout be maximized to a given view programmatically?

Can pointers or snippets be provided for the rest of the tasks in the bullet list as well, please?

Thanks for developing and maintaining this tool