Difference between revisions of "Documentation/Nightly/FAQ/Scripting"

From Slicer Wiki
Jump to: navigation, search
m (Created page with '<noinclude>__TOC__ ={{#titleparts: {{PAGENAME}} | | -1 }}=</noinclude><includeonly> ='''User FAQ: {{{1}}}'''= </includeonly> == How to grab full slice image ? ==')
 
Line 4: Line 4:
 
</includeonly>
 
</includeonly>
  
== How to grab full slice image ? ==
+
== 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
 +
 
 +
: https://gist.github.com/gortscizz/5060517

Revision as of 18:02, 1 March 2013

Home < Documentation < Nightly < FAQ < Scripting

Scripting

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

https://gist.github.com/gortscizz/5060517