# Automate printing segmentation volumes?

**URL:** <https://discourse.slicer.org/t/automate-printing-segmentation-volumes/19043>\
**Category:** Development\
**Tags:** python\
**Created:** [August 3, 2021, 3:37pm UTC](https://discourse.slicer.org/t/automate-printing-segmentation-volumes/19043 "2021-08-03T15:37:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![J1234](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@J1234](https://discourse.slicer.org/u/J1234)\
**Post date:** [August 3, 2021, 3:37pm UTC](https://discourse.slicer.org/t/automate-printing-segmentation-volumes/19043/1 "2021-08-03T15:37:40Z")

</div>

Sorry if this is painfully obvious or already covered elsewhere - I’m fairly new to both python programming and 3D Slicer. I’ve been reading through the docs all morning but so far they’ve been more confusing than helpful.

I have a directory of ~700 images (.nii) and corresponding segmentations. I want to write a simple script that will load each segmentation and print the volumes of the two segments (each segmentation contains a “tissue” and a “blood” volume).

So far I have slicer.util.loadSegmentation(“image\_number.seg.nrrd”), but I’m not sure from a python perspective how to get the output shown by the segment statistics module on the GUI interface. Can anyone point me in the right direction?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 3, 2021, 4:19pm UTC](https://discourse.slicer.org/t/automate-printing-segmentation-volumes/19043/2 "2021-08-03T16:19:48Z")

</div>

You are doing good so far. After you loaded the segmentation, run the segment statistics to get the volumes (see [complete example in the script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-volume-of-each-segment)), and write the results into any format you prefer (for example, you can write the values to a CSV file).

---

<div class="post-metadata">

**Author:** ![J1234](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@J1234](https://discourse.slicer.org/u/J1234)\
**Post date:** [August 3, 2021, 6:18pm UTC](https://discourse.slicer.org/t/automate-printing-segmentation-volumes/19043/3 "2021-08-03T18:18:12Z")

</div>

@lassoan thank you so much! Turns out I wasn’t looking in the right spot. This is exactly what I needed 🙂

However - I’ve successfully done pip install slicerio and pip install pynrrd and reloaded jupyter labs, but whenever I try to import them into a script I still get the error: ModuleNotFoundError: No module named ‘nrrd’ or ModuleNotFoundError: No module named ‘slicerio’.

Did these get changed to something else with the newest release?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 3, 2021, 6:21pm UTC](https://discourse.slicer.org/t/automate-printing-segmentation-volumes/19043/4 "2021-08-03T18:21:55Z")

</div>

> [@J1234](#):
>
> I’ve successfully done pip install slicerio and pip install pynrrd and reloaded jupyter labs, but whenever I try to import them into a script I still get the error: ModuleNotFoundError: No module named ‘nrrd’ or ModuleNotFoundError: No module named ‘slicerio’.

Probably you installed them in a different Python environment.

However, both slicerio and pynrrd are only needed when your code runs outside of Slicer’s Python environment, so you should not need them.

---

<div class="post-metadata">

**Author:** ![J1234](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@J1234](https://discourse.slicer.org/u/J1234)\
**Post date:** [August 4, 2021, 4:33am UTC](https://discourse.slicer.org/t/automate-printing-segmentation-volumes/19043/6 "2021-08-04T04:33:18Z")

</div>

I see, thank you again so much!!
