# NRRD or SEG.NRRD to PNG

**URL:** <https://discourse.slicer.org/t/nrrd-or-seg-nrrd-to-png/14459>\
**Category:** Support\
**Created:** [November 6, 2020, 6:58am UTC](https://discourse.slicer.org/t/nrrd-or-seg-nrrd-to-png/14459 "2020-11-06T06:58:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![LuckyLuke](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/luckyluke/32/8756_2.png) [@LuckyLuke](https://discourse.slicer.org/u/LuckyLuke)\
**Post date:** [November 6, 2020, 6:58am UTC](https://discourse.slicer.org/t/nrrd-or-seg-nrrd-to-png/14459/1 "2020-11-06T06:58:00Z")

</div>

So I have a request from somene to export his DICOM and NRRD files 5 of them into a set of PNG images.

Images are to be numbered and saved without losing any info, in other words either a lossless compression format or no compression at all.

What can I do now?

I have no idea why exactly he needs them saved as PNGs

---

<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, 2020, 2:13pm UTC](https://discourse.slicer.org/t/nrrd-or-seg-nrrd-to-png/14459/2 "2020-11-06T14:13:43Z")

</div>

You can load the DICOM files and export the segmentation to labelmap volumes (using the GUI), then save them into a series of png as shown at the end of [this script](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Rasterize_a_model_and_save_it_to_a_series_of_image_files) (starting from line `outputLabelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass...`).

---

<div class="post-metadata">

**Author:** ![LuckyLuke](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/luckyluke/32/8756_2.png) [@LuckyLuke](https://discourse.slicer.org/u/LuckyLuke)\
**Post date:** [November 28, 2020, 12:40pm UTC](https://discourse.slicer.org/t/nrrd-or-seg-nrrd-to-png/14459/3 "2020-11-28T12:40:43Z")

</div>

> [@lassoan](#):
>
> utputLabelmapV

dear lassoan, first of all I want to thank you for all of your help. May God bless you sir.

Concerning this script, IF I understood you correctly, I just have to make a py file that contains this and then save it by running this script?

```python
outputLabelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(seg, outputLabelmapVolumeNode, referenceVolumeNode)
outputLabelmapVolumeArray = (slicer.util.arrayFromVolume(outputLabelmapVolumeNode) * outputVolumeLabelValue).astype('int8')

# Write labelmap volume to series of TIFF files
pip_install("imageio")
import imageio
for i in range(len(outputLabelmapVolumeArray)):
    imageio.imwrite(f'{outputDir}/image_{i:03}.tiff', outputLabelmapVolumeArray[i])

```

is this loseless? Will I lose some of the information?

---

<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 28, 2020, 6:51pm UTC](https://discourse.slicer.org/t/nrrd-or-seg-nrrd-to-png/14459/4 "2020-11-28T18:51:18Z")

</div>

By saving into a series of 2D images, you lose 3D image geometry information (origin, spacing, and axis directions), so you cannot properly process the image in physical space. You also make your life harder and slow things down by storing your volume in hundreds of files instead of a single 3D image file.

---

<div class="post-metadata">

**Author:** ![LuckyLuke](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/luckyluke/32/8756_2.png) [@LuckyLuke](https://discourse.slicer.org/u/LuckyLuke)\
**Post date:** [November 28, 2020, 8:15pm UTC](https://discourse.slicer.org/t/nrrd-or-seg-nrrd-to-png/14459/5 "2020-11-28T20:15:50Z")

</div>

I understand that orientation will be lost along with spacing and axis and direction.

But someone wants to print an entire set of images for some reason and also to share them only whatever and whichever they pick.

It is something they discovered on the scan and made high resolution scan of it.

Now they don’t want to always come and take screenshots or whatever but would simply like to have the entire thing in a set of pngs and just pick whichever png they want and send it to their colleague.

that is what they told me when they said they want it as pngs sequence.

---

<div class="post-metadata">

**Author:** ![LuckyLuke](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/luckyluke/32/8756_2.png) [@LuckyLuke](https://discourse.slicer.org/u/LuckyLuke)\
**Post date:** [November 28, 2020, 8:16pm UTC](https://discourse.slicer.org/t/nrrd-or-seg-nrrd-to-png/14459/6 "2020-11-28T20:16:33Z")

</div>

so that is why my question is narrowly about the image color and resolution loss. If some information would be lost and if the lines I selected out are the correct ones. I can just enclose them in a notepadfile.py and then just run it?
