I am starting using 3D Slicer and I learnt how to load large image stack as 3D volume thanks to the ImageStacks tool provided by the SlicerMorph extension, following the Youtube tutorial.
I was wondering if it is possible there is an ImageStacks function that would load the image stack from a Python script, something similar to:
I solved it. As lassoan already mentioned in this answer, you need MRML and module logic classes to interact with any other module (find more in these slides).
After looking at ImageStack code, I ended up with the following script:
# filename is the first image path of the stack, eg "/opt/data/image-0001.tif"
filename = "/opt/data/image-0001.tif"
# Set "ImageStacks" as currently active module.
slicer.util.selectModule("ImageStacks")
# Python scripted modules
moduleWidget = slicer.modules.imagestacks.widgetRepresentation().self()
# load image stack
# User selects a file like "/opt/data/image-0001.tif"
moduleWidget.archetypeText.text = filename
# The populateFromArchetype method will populate the file list with all files
# that match the numbering pattern in that directory
moduleWidget.populateFromArchetype()
# to check correct file loading
# print(moduleWidget.logic.filePaths)
# Instantiate and add a VolumeNode to the scene.
masterVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode", "microCT_scan")
# Load the files in paths to outputNode.
moduleWidget.logic.loadVolume(outputNode = masterVolumeNode)
Dear lassoan
Thank you so much for sharing this information.
I had a question as I followed your instruction.
I had been getting ‘NoneType’ object has no attribute ‘shape’ error and I couldn’t figure out how to solve the problem.
I tried adding a scalar volume node before loading the volume but it still did not worked.
Could you please give me some advice?
Thank you always for your help.
Traceback (most recent call last):
File “”, line 1, in
File “C:/Users/njy95/AppData/Local/NA-MIC/Slicer 4.13.0-2022-02-25/NA-MIC/Extensions-30657/SlicerMorph/lib/Slicer-4.13/qt-scripted-modules/ImageStacks.py”, line 680, in loadVolume
if len(volumeArray.shape) == 3:
AttributeError: ‘NoneType’ object has no attribute ‘shape’
@hourglassnam when you get an error like that you need to trace through the code to identify where your use doesn’t match the expectations. In this case you can find the source code based on the information in the error message. You can also edit your local copy to add print statements and then restart Slicer to figure out why your data doesn’t load. If you are writing python scripts this is an important skill to develop.