# Adding or populate header in nrrd or seg.nrrd

**URL:** <https://discourse.slicer.org/t/adding-or-populate-header-in-nrrd-or-seg-nrrd/20689>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [November 18, 2021, 5:17pm UTC](https://discourse.slicer.org/t/adding-or-populate-header-in-nrrd-or-seg-nrrd/20689 "2021-11-18T17:17:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phcerdan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/phcerdan/32/1559_2.png) [@phcerdan](https://discourse.slicer.org/u/phcerdan)\
**Post date:** [November 18, 2021, 5:17pm UTC](https://discourse.slicer.org/t/adding-or-populate-header-in-nrrd-or-seg-nrrd/20689/1 "2021-11-18T17:17:31Z")

</div>

Is there any interface to add a custom header to a nrrd from Slicer, i.e: `quality:=incomplete`, or populate a predefined existing header to a Segmentation image (seg.nrrd)?

If not with GUI, with slicer python would be ok too.  
As a reference on how is done in ITK: [How to write a custom Tag to an image (NRRD or other) header - Beginner Questions - ITK](https://discourse.itk.org/t/how-to-write-a-custom-tag-to-an-image-nrrd-or-other-header/565)

I would appreciate any insight/suggestion. Thanks!

---

<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:** [November 18, 2021, 8:50pm UTC](https://discourse.slicer.org/t/adding-or-populate-header-in-nrrd-or-seg-nrrd/20689/2 "2021-11-18T20:50:39Z")

</div>

Each segment has a state (not started, in progress, completed, flagged) that you can see and modify using the GUI and also saved in the .seg.nrrd file.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/e/aefef8a053bb44476fc1b79981c32015b0cc3ecb.png)

If this is not sufficient then you can add arbitrary number of custom properties (tag:value pairs) to any segment using Python scripting (using [vtkSegment::SetTag method](http://apidocs.slicer.org/master/classvtkSegment.html#a75d0c23fc90fc1252acd14278f64c386)). These custom tags are also stored in the .seg.nrrd file.

You can read all these states and other properties in .seg.nrrd files using [slicerio](https://pypi.org/project/slicerio/) Python package (in any Python environment, not just in Slicer).

---

<div class="post-metadata">

**Author:** ![phcerdan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/phcerdan/32/1559_2.png) [@phcerdan](https://discourse.slicer.org/u/phcerdan)\
**Post date:** [February 17, 2022, 12:07am UTC](https://discourse.slicer.org/t/adding-or-populate-header-in-nrrd-or-seg-nrrd/20689/3 "2022-02-17T00:07:35Z")

</div>

For completion, this is the way to do it with proper `itk`.

```python
    import itk
    meta_dict = my_image.GetMetaDataDictionary()
    meta_dict.Set("Segment0_Name", itk.MetaDataObject[str].New(MetaDataObjectValue="horse"))
    meta_dict.Set("Segment0_ID", itk.MetaDataObject[str].New(MetaDataObjectValue="Segment_1"))
    meta_dict.Set("Segment0_Color", itk.MetaDataObject[str].New(MetaDataObjectValue="0.5300 0.6700 0.3700")) # Greenish
    meta_dict.Set("Segment0_LabelValue", itk.MetaDataObject[str].New(MetaDataObjectValue="1"))
    meta_dict.Set("Segment0_Layer", itk.MetaDataObject[str].New(MetaDataObjectValue="0"))

    meta_dict.Set("Segment1_Name", itk.MetaDataObject[str].New(MetaDataObjectValue="dog"))
    meta_dict.Set("Segment1_ID", itk.MetaDataObject[str].New(MetaDataObjectValue="Segment_2"))
    meta_dict.Set("Segment1_Color", itk.MetaDataObject[str].New(MetaDataObjectValue="0.40625 0.2187 0.6796")) # Purplish
    meta_dict.Set("Segment1_LabelValue", itk.MetaDataObject[str].New(MetaDataObjectValue="2"))
    meta_dict.Set("Segment1_Layer", itk.MetaDataObject[str].New(MetaDataObjectValue="0"))

    itk.imwrite(my_image, str(output_dir/ "my_image.nrrd"))

```
