# Call ImageStacks tool in a Python script

**URL:** <https://discourse.slicer.org/t/call-imagestacks-tool-in-a-python-script/16734>\
**Category:** Development\
**Tags:** slicermorph\
**Created:** [March 24, 2021, 1:26am UTC](https://discourse.slicer.org/t/call-imagestacks-tool-in-a-python-script/16734 "2021-03-24T01:26:10Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![gnasello](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/gnasello/32/10403_2.png) [@gnasello](https://discourse.slicer.org/u/gnasello)\
**Post date:** [March 24, 2021, 12:16pm UTC](https://discourse.slicer.org/t/call-imagestacks-tool-in-a-python-script/16734/2 "2021-03-24T12:16:51Z")

</div>

I solved it. As lassoan already mentioned in [this answer](https://discourse.slicer.org/t/how-do-i-use-python-to-call-a-transform-module-to-another-module/16711/4), you need MRML and module logic classes to interact with any other module (find more in [these slides](https://www.slicer.org/w/img_auth.php/7/79/SlicerModulesProgrammingBeyondBasics.pdf)).

After looking at [ImageStack code](https://github.com/SlicerMorph/SlicerMorph/blob/d0d9371cc783da1900e5b0923aa877d5a1586397/ImageStacks/ImageStacks.py#L593), 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)
```

---

_[View the full topic](https://discourse.slicer.org/t/call-imagestacks-tool-in-a-python-script/16734)._
