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 = slicer.app.layoutManager().threeDWidget(0).threeDView() renderWindow = view.renderWindow() renderWindow.SetAlphaBitPlanes(1) wti = vtk.vtkWindowToImageFilter() wti.SetInputBufferTypeToRGBA() wti.SetInput(renderWindow) wti.Update() vtk_data = wti.GetOutput() writer = vtk.vtkPNGWriter() writer.SetWriteToMemory(False) writer.SetInputData(vtk_data) filename = "/my_path/slicer_screenshot.png" writer.SetFileName(filename) writer.Write()
However, the above
- Does not remove the annotation labels. According to Script repository — 3D Slicer documentation, I need to execute the below to remove the labels, and this is only effective when restarting Slicer, which is inconvenient:
# Disable slice annotations immediately sliceAnnotations = slicer.modules.DataProbeInstance.infoWidget.sliceAnnotations sliceAnnotations.sliceViewAnnotationsEnabled=False sliceAnnotations.updateSliceViewFromGUI() # 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