# How to change window/level programmatically

**URL:** <https://discourse.slicer.org/t/how-to-change-window-level-programmatically/9043>\
**Category:** Development\
**Created:** [November 6, 2019, 1:30pm UTC](https://discourse.slicer.org/t/how-to-change-window-level-programmatically/9043 "2019-11-06T13:30:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![giovform](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/giovform/32/4687_2.png) [@giovform](https://discourse.slicer.org/u/giovform)\
**Post date:** [November 6, 2019, 1:30pm UTC](https://discourse.slicer.org/t/how-to-change-window-level-programmatically/9043/1 "2019-11-06T13:30:46Z")

</div>

> [@Recent improvements in window/level management](https://discourse.slicer.org/t/recent-improvements-in-window-level-management/7284/1):
>
> Ctrl + left-click-and-drag sets optimal window/level for the selected region. Pressing Escape or right-click during the drag cancels the operation.

Hi Andras, how could I set the window/level region programmatically, and apply the optimal window/level afterwards?

---

<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 6, 2019, 1:33pm UTC](https://discourse.slicer.org/t/how-to-change-window-level-programmatically/9043/2 "2019-11-06T13:33:31Z")

</div>

See example in script repository: [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Change\_window.2Flevel\_.28brightness.2Fcontrast.29\_or\_colormap\_of\_a\_volume](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Change_window.2Flevel_.28brightness.2Fcontrast.29_or_colormap_of_a_volume)

---

<div class="post-metadata">

**Author:** ![giovform](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/giovform/32/4687_2.png) [@giovform](https://discourse.slicer.org/u/giovform)\
**Post date:** [November 6, 2019, 1:55pm UTC](https://discourse.slicer.org/t/how-to-change-window-level-programmatically/9043/3 "2019-11-06T13:55:00Z")

</div>

Yes, I knew that example, but what I wanted is to auto window/level using a selected region, just like when we press ‘Ctrl’ and drag the mouse.

---

<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 6, 2019, 4:03pm UTC](https://discourse.slicer.org/t/how-to-change-window-level-programmatically/9043/4 "2019-11-06T16:03:01Z")

</div>

The logic is implemented in vtkImageHistogramStatistics. If you don’t want to use GUI then you can use that class directly from your scripts. You can have a look at the [window/level widget’s source code](https://github.com/Slicer/Slicer/blob/master/Libs/MRML/DisplayableManager/vtkMRMLWindowLevelWidget.cxx#L576) as an example how it is used there.

---

<div class="post-metadata">

**Author:** ![giovform](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/giovform/32/4687_2.png) [@giovform](https://discourse.slicer.org/u/giovform)\
**Post date:** [November 7, 2019, 4:36pm UTC](https://discourse.slicer.org/t/how-to-change-window-level-programmatically/9043/5 "2019-11-07T16:36:30Z")

</div>

The code snippet for anyone that might be interested (selecting a rectangle at the specified positions on the background of the Green slice viewer and applying Window Level):

```
widget = slicer.vtkMRMLWindowLevelWidget()
widget.SetSliceNode(getNode('vtkMRMLSliceNodeGreen'))
widget.SetMRMLApplicationLogic(slicer.app.applicationLogic())
widget.UpdateWindowLevelFromRectangle(0, [60,60], [45,45])
```
