Tips for getting started loading and batch segmenting files with Slicer in python?

I’m new to Slicer and am working on a project trying to automate a few Slicer steps for many files by creating a python script and then running it without opening the Slicer GUI.

I’ve been reading through sections in the slicer docs in particular the “Run a Python script file in the Slicer environment” seems to be what I’m trying to do.

The part I’m stuck on is the very first step of how to load a file (tif file as a Volume) in slicer through python. I’d really appreciate any tips or example code on how to do this part!

For bigger picture context, I have a folder with dozens of .tif files from CTs. I’d like to run a script that will for each .tif file replicate the following steps I’ve been doing in the Slicer GUI:

  • Load the tif file as a Volume (Add data button)
  • Segment based on a threshold
  • Select remove island based on a known location (background CT scan equipment)
  • Save the segmentation quantification & a .stl file of the 3d model


That process sounds very feasible. You’ll need to put together snippets of functionality from the script repository. I saw there weren’t specific batch processing examples so I put in a few examples in this PR.

1 Like

Thank you!
That script got me well ahead and working.
I do have two follow up questions now, but they’re rather distinct so I’ve made them new questions:

  1. Remove largest island functionality?
  2. Export volume (or segment?) to stl file with python

Check this script example for running island tool and exporitng the segmentation and writing to the disk:

1 Like