# How to export color node as color table

**URL:** <https://discourse.slicer.org/t/how-to-export-color-node-as-color-table/29134>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [April 26, 2023, 9:01am UTC](https://discourse.slicer.org/t/how-to-export-color-node-as-color-table/29134 "2023-04-26T09:01:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dyollb](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dyollb/32/16686_2.png) [@dyollb](https://discourse.slicer.org/u/dyollb)\
**Post date:** [April 26, 2023, 9:01am UTC](https://discourse.slicer.org/t/how-to-export-color-node-as-color-table/29134/1 "2023-04-26T09:01:11Z")

</div>

I would like to convert a slicer .seg.nrrd segmentation into a labelmap + color table, so I can use it downstream in other applications. I figured out how to convert the segmentation to a labelmap and I can see the color node under the Data (All nodes) section. Is there a way I can export it to the color table format, described e.g. [here](https://slicer.readthedocs.io/en/latest/developer_guide/modules/colors.html#color-table-file-format-txt-ctbl)?

The slicer color table format is easy to parse (and already supported by the tool I am using).

---

<div class="post-metadata">

**Author:** ![dyollb](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dyollb/32/16686_2.png) [@dyollb](https://discourse.slicer.org/u/dyollb)\
**Post date:** [April 27, 2023, 2:31pm UTC](https://discourse.slicer.org/t/how-to-export-color-node-as-color-table/29134/2 "2023-04-27T14:31:27Z")

</div>

I guess I could access the labelmap node via `getNode("name of node")`. I didn’t see what api I could use to export the color table (or more importantly the index and name mapping).

As a workaround I used the `slicerio` module to export the segmentation as a labelmap and save the color table (by hand):

```python
import slicerio
import nrrd
import random
import numpy as np
from pathlib import Path

input_filename = "/Users/lloyd/Downloads/segIEMap.seg.nrrd"
output_filename = "/Users/lloyd/Downloads/segIEMap.nrrd"

voxels, header = nrrd.read(input_filename)
segmentation_info = slicerio.read_segmentation_info(input_filename)

number_of_segments = len(segmentation_info["segments"])
print(f"Number of segments: {number_of_segments}")

segment_names = slicerio.segment_names(segmentation_info)

segment_names_to_labels = []
for idx, name in enumerate(segment_names, start=1):
    segment_names_to_labels.append((name, idx))

print(segment_names_to_labels)

voxels3, map = slicerio.extract_segments(voxels, header, segmentation_info, segment_names_to_labels)
nrrd.write(output_filename, voxels3)

with open(Path(output_filename).with_suffix(".txt"), "w") as f:
    print("# Color table file", file=f)
    print(f"# {len(segment_names_to_labels) + 1} values", file=f)
    print("0 Background 0 0 0 0", file=f)
    for (name, idx) in segment_names_to_labels:
        print(f"{idx} {name} {random.randint(0, 255)} {random.randint(0, 255)} {random.randint(0, 255)} 255", file=f)

```
