# Change Color of Segmentation by Script

**URL:** <https://discourse.slicer.org/t/change-color-of-segmentation-by-script/3170>\
**Category:** Support\
**Created:** [June 13, 2018, 3:09pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-by-script/3170 "2018-06-13T15:09:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![trnhx001](https://avatars.discourse-cdn.com/v4/letter/t/ed655f/32.png) [@trnhx001](https://discourse.slicer.org/u/trnhx001)\
**Post date:** [June 13, 2018, 3:09pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-by-script/3170/1 "2018-06-13T15:09:05Z")

</div>

Operating system: Window 7  
Slicer version: 4.8.1

Hi,

I am trying to write a program to load two segmentation of Carotid done by two different people on the same volume, then I want to save images of all slices from axial view to a folder.

I could do most of the thing except changing the color of the segmentations. The reason is that I want to compare between two segmentations. It is hard to compare them when they are in the same color.

Is there anyway to write a script to change the color of the finished segmentation? I tried the below code, but it seems like it does not work this way:

segmentationNode = getNode(‘Segmentation’)  
segmentationNode.SetColor(1,1,0)

or

segmentationDisplayNode = segmentationNode.GetDisplayNode()  
segmentationDisplayNode.SetColor(1,1,0)

Thank you for all of your help.

---

<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:** [June 13, 2018, 7:28pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-by-script/3170/2 "2018-06-13T19:28:45Z")

</div>

You need to set color for individual segments. For example, set first segment color to red:

```
segmentation = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLSegmentationNode').GetSegmentation()
segmentation.GetNthSegment(0).SetColor(1,0,0)
```

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [September 13, 2021, 11:00pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-by-script/3170/3 "2021-09-13T23:00:36Z")

</div>

Hi Andras,  
I am setting color like this  
scalpSeg = segmentationNode.GetSegmentation().GetSegment(scalpSelector)  
scalpSeg.SetColor(255,192,203)  
but its not working even if I change the color values. Not giving any error. code is working but cant visualize in the segment editor window

Thank you

Regards,  
Saima Safdar

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [September 13, 2021, 11:52pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-by-script/3170/4 "2021-09-13T23:52:40Z")

</div>

I believe setting segment color takes values from 0 to 1 where 255 is equivalent to 1. So setting to 255,192,203 is actually setting it to 1,1,1.

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [September 14, 2021, 3:49am UTC](https://discourse.slicer.org/t/change-color-of-segmentation-by-script/3170/5 "2021-09-14T03:49:03Z")

</div>

> [@jamesobutler](#):
>
> 0 to 1

Hi,  
If I have red =255  
green = 192  
and yellow = 203

so what will be the values in o to 1 range.

Regards,  
Saima Safdar

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [September 14, 2021, 9:29pm UTC](https://discourse.slicer.org/t/change-color-of-segmentation-by-script/3170/6 "2021-09-14T21:29:57Z")

</div>

You have to divide each of the colors by 255
