# Save Segmentation as DICOM file with Simple ITK and Python

**URL:** https://discourse.itk.org/t/save-segmentation-as-dicom-file-with-simple-itk-and-python/5593
**Category:** Beginner Questions
**Tags:** python, dicom, simpleitk
**Created:** [December 23, 2022, 4:01pm UTC](https://discourse.itk.org/t/save-segmentation-as-dicom-file-with-simple-itk-and-python/5593 "2022-12-23T16:01:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![riba](https://discourse.itk.org/letter_avatar_proxy/v4/letter/r/4da419/32.png) [@riba](https://discourse.itk.org/u/riba)
#### Post date: [December 23, 2022, 4:01pm UTC](https://discourse.itk.org/t/save-segmentation-as-dicom-file-with-simple-itk-and-python/5593/1 "2022-12-23T16:01:23Z")

</div>

I want to write the image an numpy array (with zeros and ones only) which is the result of the segmentation of a CT image.

How can I save it using sitk? I don’t know how to set some of the metadata as: high bit, bits allocated, bits stored, pixel representation…

Also, should I cast my image to what type? sitk.sitkUInt16?

Some code I have tried:

```auto
img = sitk.GetImageFromArray(np.asarray(array).astype(np.uint16))

img = sitk.Cast(img , sitk.sitkUInt16)

writer = sitk.ImageFileWriter()

img.SetDirection(original_volume.GetDirection())
img.SetOrigin(original_volume.GetOrigin())
img.SetSpacing(original_volume.GetSpacing())

writer.SetFileName(output_PATH)
writer.Execute(img)

```

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [December 23, 2022, 5:05pm UTC](https://discourse.itk.org/t/save-segmentation-as-dicom-file-with-simple-itk-and-python/5593/2 "2022-12-23T17:05:34Z")

</div>

Hello @riba,

Please see [this SimpleITK example](https://simpleitk.readthedocs.io/en/master/link_DicomSeriesFromArray_docs.html) which does what you want.

---

<div class="post-metadata">

### Author: ![riba](https://discourse.itk.org/letter_avatar_proxy/v4/letter/r/4da419/32.png) [@riba](https://discourse.itk.org/u/riba)
#### Post date: [December 28, 2022, 10:42am UTC](https://discourse.itk.org/t/save-segmentation-as-dicom-file-with-simple-itk-and-python/5593/3 "2022-12-28T10:42:38Z")

</div>

Hi @zivy,

thanks for the answer.

However, some metadata are still not recorded, such as the spacing even when doing

`img.SetSpacing(original_volume.GetSpacing())`

or, for example,  
`img.SetMetaData('0028|0030', '2.5\2.5')`

NOTE:  
Opening the saved DICOM file in 3D slicer I can see all the metadata I defined, but ITK SNAP cannot read some metadata (like pixel spacing) and if I use the validator on [All CIODs – DICOM Standard Browser](https://dicom.innolitics.com/ciods) it also says that some metadata tags are missing

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [December 28, 2022, 2:02pm UTC](https://discourse.itk.org/t/save-segmentation-as-dicom-file-with-simple-itk-and-python/5593/4 "2022-12-28T14:02:47Z")

</div>

Hello @riba,

Setting the meta-data explicitly (i.e. `img.SetMetaData('0028|0030', '2.5\2.5')`) is ignored. ITK/SimpleITK will use the spacing from the actual image. The `SetSpacing` should do what you want. It does work for the images I’ve worked with, so not sure what is going on with your code.

ITK-SNAP does read the pixel spacing, it just doesn’t display it under the “Metadata” tab of the image information. You will find it under the “Info” tab.

With respect to the complaints about missing tags, the example is generic and the documentation states that it is up to the developer to create the relevant tags and set them correctly, comment starting with “# IMPORTANT: There are many DICOM tags that need to be updated when you modify…”

Generally speaking, saving a segmentation this way is a “hack”. The official way is to use DICOM-SEG. If you want to go that route, I’d recommend looking into using [highdicom](https://github.com/herrmannlab/highdicom) or [dcmqi](https://github.com/QIICR/dcmqi).
