# NRRD volume - Save a slice

**URL:** <https://discourse.slicer.org/t/nrrd-volume-save-a-slice/26489>\
**Category:** Support\
**Tags:** registration, python, nrrd\
**Created:** [November 29, 2022, 9:28am UTC](https://discourse.slicer.org/t/nrrd-volume-save-a-slice/26489 "2022-11-29T09:28:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucas\_sd](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lucas_sd/32/17087_2.png) [@lucas\_sd](https://discourse.slicer.org/u/lucas_sd)\
**Post date:** [November 29, 2022, 9:28am UTC](https://discourse.slicer.org/t/nrrd-volume-save-a-slice/26489/1 "2022-11-29T09:28:18Z")

</div>

Hi everybody,

Im working in a new module and I have a volume NRRD. I need to save just one slice, like [:,:,Zn], in a new NRRD volume. Is there a specific command to do this? I need to get the node first?

Thanks, Lucas.

---

<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:** [December 1, 2022, 1:58am UTC](https://discourse.slicer.org/t/nrrd-volume-save-a-slice/26489/2 "2022-12-01T01:58:00Z")

</div>

You can crop the volume using Crop volume module (see code snippet [here](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#fit-markups-roi-to-volume)) or you can use numpy indexing to get a region of a volume and then create a new volume from that using [slicer.util.addVolumeFromArray](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.addVolumeFromArray). You need to copy the IJK to RAS matrix from the original volume but don’t forget to set the origin to the position of the slice that you extracted (the original volume’s origin is the position of the first slice in the original volume).

---

<div class="post-metadata">

**Author:** ![lucas\_sd](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lucas_sd/32/17087_2.png) [@lucas\_sd](https://discourse.slicer.org/u/lucas_sd)\
**Post date:** [December 1, 2022, 3:13am UTC](https://discourse.slicer.org/t/nrrd-volume-save-a-slice/26489/3 "2022-12-01T03:13:33Z")

</div>

Hi Andras,

Thanks for your help, the idea was to get a .png image of a plane of a volume. Finally, and maybe not in the most efficient way, I did this:

```auto
readdata, header = nrrd.read(volume.nrrd)
image38 = np.asarray(readdata[:,38,:]).astype(int) #38, the specific slice.
final = Image.fromarray(manipulating_nrrd_contrast(image38, 128)) #Create an image and some work with the contrast.
final.save(f'image38.png')

```

I know .png is not recommended and its different, but for my its a requirement.

---

<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:** [December 1, 2022, 2:23pm UTC](https://discourse.slicer.org/t/nrrd-volume-save-a-slice/26489/4 "2022-12-01T14:23:17Z")

</div>

If you don’t need the correct image geometry (origin, spacing, axis directions) then it gets really simple:

```python
slice = slicer.util.array(volumeNode)[:,38,:]
Image.fromarray(slice).save('image38.png')

```

---

<div class="post-metadata">

**Author:** ![lucas\_sd](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lucas_sd/32/17087_2.png) [@lucas\_sd](https://discourse.slicer.org/u/lucas_sd)\
**Post date:** [December 6, 2022, 10:28am UTC](https://discourse.slicer.org/t/nrrd-volume-save-a-slice/26489/5 "2022-12-06T10:28:50Z")

</div>

Hi Andras,

Thanks! I wonder if its possible to fix by code the contrast and orientation, before save the file.  
Are there some commands to fix this and set it like properties?

Thanks, Lucas.

---

<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:** [December 6, 2022, 4:17pm UTC](https://discourse.slicer.org/t/nrrd-volume-save-a-slice/26489/6 "2022-12-06T16:17:16Z")

</div>

By converting into PNG you may lose information. In theory you may be able to save a 16-bit grayscale image into PNG, but most software cannot work with such images. I would recommend to use a medical image file format (nrrd, mha, …) that don’t have issue with storing the native image contrast/brightness.

I don’t know what you mean exactly by “fixing the orientation”. The input image is already correctly oriented. You can also crop and resample it and save it into a medical image file format, which maintains size, position, and orientation of the image slice. If you use a consumer file format, such as PNG, then there is no standard way of storing the slice position or orientation.

It seems that all your issues come from trying to use a file format that is not appropriate for the data you have. I would recommend to keep using the nrrd format and process it with software tools and libraries that are suitable for medical image computing. Check out torchio, monai, monailabel, etc.
