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

**URL:** https://discourse.slicer.org/t/tips-for-getting-started-loading-and-batch-segmenting-files-with-slicer-in-python/24850
**Category:** Support
**Tags:** python
**Created:** [August 21, 2022, 7:58am UTC](https://discourse.slicer.org/t/tips-for-getting-started-loading-and-batch-segmenting-files-with-slicer-in-python/24850 "2022-08-21T07:58:28Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Mike\_Flanigan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mike_flanigan/32/14586_2.png) [@Mike\_Flanigan](https://discourse.slicer.org/u/Mike_Flanigan)
#### Post date: [August 21, 2022, 7:58am UTC](https://discourse.slicer.org/t/tips-for-getting-started-loading-and-batch-segmenting-files-with-slicer-in-python/24850/1 "2022-08-21T07:58:28Z")

</div>

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](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html) 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

Thanks!

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [August 22, 2022, 3:23pm UTC](https://discourse.slicer.org/t/tips-for-getting-started-loading-and-batch-segmenting-files-with-slicer-in-python/24850/2 "2022-08-22T15:23:36Z")

</div>

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](https://github.com/Slicer/Slicer/pull/6508/files).

---

<div class="post-metadata">

### Author: ![Mike\_Flanigan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mike_flanigan/32/14586_2.png) [@Mike\_Flanigan](https://discourse.slicer.org/u/Mike_Flanigan)
#### Post date: [September 5, 2022, 10:52pm UTC](https://discourse.slicer.org/t/tips-for-getting-started-loading-and-batch-segmenting-files-with-slicer-in-python/24850/3 "2022-09-05T22:52:33Z")

</div>

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?](https://discourse.slicer.org/t/remove-largest-island-functionality/25110)
2. [Export volume (or segment?) to stl file with python](https://discourse.slicer.org/t/export-volume-or-segment-to-stl-file-with-python/25111)

---

<div class="post-metadata">

### Author: ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)
#### Post date: [September 6, 2022, 5:17am UTC](https://discourse.slicer.org/t/tips-for-getting-started-loading-and-batch-segmenting-files-with-slicer-in-python/24850/4 "2022-09-06T05:17:58Z")

</div>

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

> **[GitHub - SlicerMorph/Scripts](https://github.com/SlicerMorph/Scripts#2-run-an-image-processing-pipeline-on-a-folder-of-volumes)**
>
> Contribute to SlicerMorph/Scripts development by creating an account on GitHub.
