Documentation/4.3/FAQ/Scripting
From Slicer Wiki
Home < Documentation < 4.3 < FAQ < Scripting
For the latest Slicer documentation, visit the read-the-docs. |
Contents
Scripting
Should I learn Python 2.X or 3.X ?
Since Slicer is currently built against python 2.7.3, we suggest you to learn python 2.X.
To learn more about the difference:
How to grab full slice image (+-volume, +-model) ?
A Few General Approaches
There are multiple ways to access image data from one or all of the slice views.
VTK Window to Image Function (vtk.vtkWindowToImageFilter()
- # Example to grab image from red slice view and output as TIF
- lm = slicer.app.layoutManager()
- redWidget = lm.sliceWidget('Red')
- redView = redWidget.sliceView()
- wti = vtk.vtkWindowToImageFilter()
- wti.SetInput(redView.renderWindow())
- wti.Update()
- w = vtk.vtkTIFFWriter()
- w.SetInputConnection(wti.GetOutputPort())
- w.SetFileName('tmp/test.tif')
- w.Write()
Direct access to widget data using
- # Example grabs image from red slice view and outputs as PNG
- lm = slicer.app.layoutManager()
- redWidget = lm.sliceWidget('Red')
- im = redWidget.imageData()
- w = vtk.vtkPNGWriter()
- w.SetInput(im)
- w.SetFileName('tmp/test.png')
- w.Write()
QImages using the qt.QPixmap.grabWidget function
- # example to grab all views and store in 'image'
image = qt.QPixmap.grabWidget(slicer.util.mainWindow()).toImage()
- # example to grab 3d view, store in tdv, then output as a png image file
tdv = lm.threeDWidget(0).threeDView() qt.QPixmap().grabWidget(tdv).toImage().save('/tmp/test.png')
Notes
This is also useful for slicing up whatever is shown (eg. voxelizing model data) by iterating this over the dimensions of the volume/model
In order to view models in the slice view make sure to activate the "Slice Intersections Visible" property of the model
Specific Example
The following script iterates through the red slice view and outputs each slice as a .png image. It was originally designed to output volume+model data together slice-by-slice