Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
3DSlicer. Python Scripting: How to grab full slice image
Originally Written Using 3DSlicer v 4.2.2-1
This script does a screen capture of every z-slice in the red slice panel of 3D slicer.
-This was originally developed to slice up volume & model data displayed together
-Slices of model data are shown after activating the display property "Slice Intersections Visible" in the model module
-You can output just volume or just model data by changing display visibility in the red slice view
-Currently no gui elements. Must hard-code variables, volname and dir, in the script below.
-Currently outputs PNG images
To change output to a different (eg. to lossless .tif) format:
change vtkPNGWriter to vtkTIFFWriter
change ".png" to ".tif"
Add a simple QT gui interface for defining file paths and different output image formats
Before Running
1) Load at least one volume (used to define dimensions of output images)
Set volname variable (below) to the volume name in Slicer
2) Set View to Red Pane Only (View:Layout:Red Slice Only)
3) Manually set the output directory (dir) below
Must set these two variables
volname = 'NameOfTheVolumeInSlicer'
#Grab red panel, volume node, and volume dimensions array (dims)
sln = slicer.util.getNode('vtkMRMLSliceNodeRed')
n = getNode(volname)
im = n.GetImageData()
dims = list(im.GetDimensions())
# dims[0] is x, dims[1] is y, dims [2] is Z
lm =
redWidget = lm.sliceWidget('Red')
redView = redWidget.sliceView()
slncw = redWidget.sliceController()
for num in xrange(0,dims[2]):
wti = vtk.vtkWindowToImageFilter()
w = vtk.vtkPNGWriter()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.