# How to get pixel value and coordinate from nrrd file

**URL:** https://discourse.slicer.org/t/how-to-get-pixel-value-and-coordinate-from-nrrd-file/7030
**Category:** Support
**Tags:** vtk, python, nrrd, itk
**Created:** [June 4, 2019, 11:36pm UTC](https://discourse.slicer.org/t/how-to-get-pixel-value-and-coordinate-from-nrrd-file/7030 "2019-06-04T23:36:15Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![kscript](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@kscript](https://discourse.slicer.org/u/kscript)
#### Post date: [June 4, 2019, 11:36pm UTC](https://discourse.slicer.org/t/how-to-get-pixel-value-and-coordinate-from-nrrd-file/7030/1 "2019-06-04T23:36:15Z")

</div>

So I make nrrd file. In this file has Annotation Data using 3D Slicer.

I want to get pixel coordinate and pixel value from this file.

I modified the code with reference to the source of this link, but I did not get any results.

[python dicom – getting pixel value from dicom file](https://stackoverflow.com/questions/39687295/python-dicom-getting-pixel-value-from-dicom-file)

I really couldn’t find any information so any helpful resources would be appreciated as well. Thank you.

---

<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 5, 2019, 1:26am UTC](https://discourse.slicer.org/t/how-to-get-pixel-value-and-coordinate-from-nrrd-file/7030/2 "2019-06-05T01:26:33Z")

</div>

These examples in the script repository should help:

[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Modify\_voxels\_in\_a\_volume](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Modify_voxels_in_a_volume)

[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get\_volume\_voxel\_coordinates\_from\_markup\_fiducial\_RAS\_coordinates](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_volume_voxel_coordinates_from_markup_fiducial_RAS_coordinates)

[https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get\_markup\_fiducial\_RAS\_coordinates\_from\_volume\_voxel\_coordinates](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_markup_fiducial_RAS_coordinates_from_volume_voxel_coordinates)

---

<div class="post-metadata">

### Author: ![kscript](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@kscript](https://discourse.slicer.org/u/kscript)
#### Post date: [June 5, 2019, 2:41am UTC](https://discourse.slicer.org/t/how-to-get-pixel-value-and-coordinate-from-nrrd-file/7030/3 "2019-06-05T02:41:21Z")

</div>

Thanks reply.

This code need to use python interaction in 3D Slicer. Right??

---

<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 5, 2019, 2:55am UTC](https://discourse.slicer.org/t/how-to-get-pixel-value-and-coordinate-from-nrrd-file/7030/4 "2019-06-05T02:55:29Z")

</div>

> [@kscript](#):
>
> This code need to use python interaction in 3D Slicer. Right??

You need to run the code in Slicer’s Python environment. You probably don’t need to show Slicer GUI - you can [run your script from the command-line](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#How_can_I_run_slicer_operations_from_a_batch_script.3F).

---

<div class="post-metadata">

### Author: ![kscript](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@kscript](https://discourse.slicer.org/u/kscript)
#### Post date: [June 5, 2019, 3:15am UTC](https://discourse.slicer.org/t/how-to-get-pixel-value-and-coordinate-from-nrrd-file/7030/5 "2019-06-05T03:15:05Z")

</div>

Oh I can see array!

And i want to know each array’s all information.

now we can see like this.  
[[[0 0 0 …, 0 0 0]  
[0 0 0 …, 0 0 0]  
[0 0 0 …, 0 0 0]

i want to remove … this part and i want to print all arrya elements.

and i save result to txt file. but result is same.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/3/238b1c2919ac499c1c36ae4ca44cb223cee58d84.png)

---

<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 5, 2019, 2:01pm UTC](https://discourse.slicer.org/t/how-to-get-pixel-value-and-coordinate-from-nrrd-file/7030/6 "2019-06-05T14:01:57Z")

</div>

Writing out 3D volume to text file is a bad idea (large file, slow to read/write, potential data loss due to limited precision), but here is a complete example just to show how easy it is to do:

```auto
# Get some image data
import SampleData
volumeNode=SampleData.SampleDataLogic().downloadMRHead()

# Get voxels as numpy array
volumeArray=slicer.util.arrayFromVolume(volumeNode)

# Write voxels to text file
import numpy as np
with file('c:/tmp/test.txt', 'w') as outfile:
  for data_slice in volumeArray:
    np.savetxt(outfile, data_slice, fmt='%-7.2f')

```

If you want to write in a different format then search for Python code examples on the web.
