Skip to content

Instantly share code, notes, and snippets.

Embed
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"
ToDo:
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'
dir="C:\\Users\\User\\Desktop\\SlicerScripts\\seq"
#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 = slicer.app.layoutManager()
redWidget = lm.sliceWidget('Red')
redView = redWidget.sliceView()
slncw = redWidget.sliceController()
slncw.close()
redWidget.setFixedSize(dims[0],dims[1])
sln.SetFieldOfView(dims[0],dims[1],1)
sln.SetDimensions(dims[0],dims[1],1)
for num in xrange(0,dims[2]):
sln.SetSliceOffset(num)
slicer.app.processEvents()
wti = vtk.vtkWindowToImageFilter()
wti.SetInput(redView.renderWindow())
wti.Update()
w = vtk.vtkPNGWriter()
w.SetInputConnection(wti.GetOutputPort())
w.SetFileName(dir+"\\"+str(num)+".png")
w.Write()
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.