# Physical distances in the slice view via python

**URL:** <https://discourse.slicer.org/t/physical-distances-in-the-slice-view-via-python/31593>\
**Category:** Development\
**Created:** [September 6, 2023, 3:15pm UTC](https://discourse.slicer.org/t/physical-distances-in-the-slice-view-via-python/31593 "2023-09-06T15:15:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![laurabc](https://avatars.discourse-cdn.com/v4/letter/l/ea5d25/32.png) [@laurabc](https://discourse.slicer.org/u/laurabc)\
**Post date:** [September 6, 2023, 3:15pm UTC](https://discourse.slicer.org/t/physical-distances-in-the-slice-view-via-python/31593/1 "2023-09-06T15:15:05Z")

</div>

Hello all,

I want to measure physical distances in a slice view (I previously reformatted the volume and zoomed in). I know I can set markers and get the physical distance with the line tool, but I would like to do it programmatically via a python script. Is there a way to transform the coordinates to physical distances? Or to know the size of a pixel in the slice view? Or to know the physical size of the slice view? Any option that would allow me to obtain physical units.

Thanks  
Laura

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [September 6, 2023, 6:28pm UTC](https://discourse.slicer.org/t/physical-distances-in-the-slice-view-via-python/31593/2 "2023-09-06T18:28:09Z")

</div>

The simplest way would be to put some kind of Markups points at the locations you want to measure between. This could be control points for a MarkupsCurve or a MarkupsPointList, for example. Then the coordinates of these points in physical units are easily accessible and you can just calculate the euclidean distance between them.

```auto
import numpy as np
markupsNode = getNode('MyPoints')
pos0 = np.zeros(3) # initialize variable to hold the coordinates of the first point
markupsNode.GetNthControlPointPositionWorld(0, pos0) # get coord of first point
pos1 = np.zeros(3) # initialize variable to hold coord of second point
markupsNode.GetNthControlPointPositionWorld(1, pos1) # get coord of second point
# Calculate the physical distance between these points (in millimeters because Slicer world coordinates are in mm)
distanceBetweenPointsMm = np.linalg.norm(pos1-pos2) 

```

The placed points can be in a single slice view or placed in 3D or placed on different slices, etc., it doesn’t matter. Slicer keeps track of everything in a physical 3D spatial representation.
