# Change label color using python script

**URL:** <https://discourse.slicer.org/t/change-label-color-using-python-script/213>\
**Category:** Development\
**Created:** [April 27, 2017, 1:48pm UTC](https://discourse.slicer.org/t/change-label-color-using-python-script/213 "2017-04-27T13:48:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [April 27, 2017, 1:48pm UTC](https://discourse.slicer.org/t/change-label-color-using-python-script/213/1 "2017-04-27T13:48:54Z")

</div>

Dear all,

how can I combine all the colors in the “label” image to one color using python script.

Thanks

IIbraheem

---

<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:** [April 27, 2017, 2:06pm UTC](https://discourse.slicer.org/t/change-label-color-using-python-script/213/2 "2017-04-27T14:06:06Z")

</div>

Get the image data from the labelmap volume node, and apply [vtkImageThreshold](http://www.vtk.org/doc/nightly/html/classvtkImageThreshold.html) filter.

---

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [April 27, 2017, 6:58pm UTC](https://discourse.slicer.org/t/change-label-color-using-python-script/213/3 "2017-04-27T18:58:42Z")

</div>

Thanks Andras. I will try this. I thought there maybe an easier way by using the “ChangeLabelEffect” from the editor module.

---

<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:** [April 27, 2017, 7:18pm UTC](https://discourse.slicer.org/t/change-label-color-using-python-script/213/4 "2017-04-27T19:18:24Z")

</div>

If you want to modify labels from your own script then manipulating the voxels using numpy is probably the simplest. For example, to change all non-zero voxels to 2:

```
a = slicer.util.array('Output Volume-label')
a[a>0] = 2

```

Using the vtkImageThreshold filter would be about 5-6 lines.

If you manipulate segmentations using GUI then I would recommend to use Segment Editor module instead of the old, relatively limited Editor module.

---

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [April 27, 2017, 7:39pm UTC](https://discourse.slicer.org/t/change-label-color-using-python-script/213/5 "2017-04-27T19:39:38Z")

</div>

Thanks for the tips. I think numpy is the fastest way to do this. Could you please share how to write the array back to the volume?  
`lb= slicer.util.getNode('input-label')`  
`lbd = lb.GetImageData()`  
`lbd[lbd>0] = 2`  
I can not find sothomig like  
`lb.SettImageData(lbd)`

---

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [April 27, 2017, 7:45pm UTC](https://discourse.slicer.org/t/change-label-color-using-python-script/213/6 "2017-04-27T19:45:16Z")

</div>

found it 😁  
I forgot to update the dispaly.  
these commands are enough:  
`lb= slicer.util.getNode('input-label')`  
`lb[lb>0] = 2`

---

<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:** [April 27, 2017, 9:23pm UTC](https://discourse.slicer.org/t/change-label-color-using-python-script/213/7 "2017-04-27T21:23:12Z")

</div>

Yes, if you just toggle the slice slider the update volume will show up.

But if you want to do it automatically from a script you need to inform VTK/Slicer that numpy has changed the binary data so it will render automatically.

So you need something like this to trigger a redraw

```auto
a = slicer.util.array('Output Volume-label')
a[a>0] = 2
node = slicer.util.getNode('Output Volume-label')
node.GetImageData().GetPointData().GetScalars().Modified()
node.Modified()

```

---

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [April 28, 2017, 9:08pm UTC](https://discourse.slicer.org/t/change-label-color-using-python-script/213/8 "2017-04-28T21:08:02Z")

</div>

many thanks for the code. It helps a lot.
