# How to save dicom image or segmentation mask to a .py file or .nii file?

**URL:** <https://discourse.slicer.org/t/how-to-save-dicom-image-or-segmentation-mask-to-a-py-file-or-nii-file/12108>\
**Category:** Support\
**Created:** [June 19, 2020, 5:13am UTC](https://discourse.slicer.org/t/how-to-save-dicom-image-or-segmentation-mask-to-a-py-file-or-nii-file/12108 "2020-06-19T05:13:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SummerZ2020](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/summerz2020/32/7137_2.png) [@SummerZ2020](https://discourse.slicer.org/u/SummerZ2020)\
**Post date:** [June 19, 2020, 5:13am UTC](https://discourse.slicer.org/t/how-to-save-dicom-image-or-segmentation-mask-to-a-py-file-or-nii-file/12108/1 "2020-06-19T05:13:19Z")

</div>

I used the “segment editor” to get the bone tissue just now. and then I need save the segmentation mask to a .py file. I did not find how to do it in GUI, so I tried to do it with python script.I tried to get the data from node and save the numpy array to .py file. but i found seems there is not data array in the saved test.py file. here is the python code :  
import numpy as np  
bone\_arr = np.array(‘Segment\_1’)#Segment\_1 is the bone mask  
np.save(‘d:\test.npy’,bone\_arr)  
And then I tried to use “savenode” save the segmentation node to .py file. But I failed, seems the node can not be saved as .py file. And I do not know how to get the data array (segmentation)from the node easily in python script.Or can i save the segmentation mask to the .nii or .py file in GUI?  
thank you in advance.

---

<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:** [June 19, 2020, 9:15pm UTC](https://discourse.slicer.org/t/how-to-save-dicom-image-or-segmentation-mask-to-a-py-file-or-nii-file/12108/2 "2020-06-19T21:15:27Z")

</div>

You need to export to a labelmap first, which you can do in the Segmentations module panel shown below. Then you can do:

```auto
>>> a = array("Segmentation-label")
>>> numpy.save('/tmp/x.py', a)

```

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/f/8f706f6fdd556c0e32bf024a393a90e71ff48150.png)

---

<div class="post-metadata">

**Author:** ![SummerZ2020](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/summerz2020/32/7137_2.png) [@SummerZ2020](https://discourse.slicer.org/u/SummerZ2020)\
**Post date:** [June 22, 2020, 1:33am UTC](https://discourse.slicer.org/t/how-to-save-dicom-image-or-segmentation-mask-to-a-py-file-or-nii-file/12108/3 "2020-06-22T01:33:04Z")

</div>

Your answer works. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![Deep\_Learning](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/deep_learning/32/15345_2.png) [@Deep\_Learning](https://discourse.slicer.org/u/Deep_Learning)\
**Post date:** [August 31, 2023, 3:26pm UTC](https://discourse.slicer.org/t/how-to-save-dicom-image-or-segmentation-mask-to-a-py-file-or-nii-file/12108/5 "2023-08-31T15:26:08Z")

</div>

Bump…  
Loading two nii.gz (volume and segmentation(only 1 segment)).

Modifying the segmentation.  
Converting to binary labelmap.  
Export to file (.nii.gz).

Exported file is much smaller than the volume/orig segmentation.  
Seems to be limited by the segmentation bounding box.

Is it possible to export the segmentation with the original dimensions. (ie compatable with pyradiomics)?

---

<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 31, 2023, 3:33pm UTC](https://discourse.slicer.org/t/how-to-save-dicom-image-or-segmentation-mask-to-a-py-file-or-nii-file/12108/6 "2023-08-31T15:33:22Z")

</div>

Saving the full dimensions should be the default now, so try with the latest version.

---

<div class="post-metadata">

**Author:** ![Deep\_Learning](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/deep_learning/32/15345_2.png) [@Deep\_Learning](https://discourse.slicer.org/u/Deep_Learning)\
**Post date:** [August 31, 2023, 5:51pm UTC](https://discourse.slicer.org/t/how-to-save-dicom-image-or-segmentation-mask-to-a-py-file-or-nii-file/12108/7 "2023-08-31T17:51:07Z")

</div>

Ughhh… Thank you…

I cannot duplicate it today.  
But I have a bunch of cropped segmentations from yesterday…

Not sure how it happened, but it did…  
Was able to reset the geometry one by one…
