# Label image defects after saving

**URL:** https://discourse.slicer.org/t/label-image-defects-after-saving/20062
**Category:** Support
**Tags:** python, labelmap
**Created:** [October 7, 2021, 9:05pm UTC](https://discourse.slicer.org/t/label-image-defects-after-saving/20062 "2021-10-07T21:05:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![MachadoL](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/machadol/32/3372_2.png) [@MachadoL](https://discourse.slicer.org/u/MachadoL)
#### Post date: [October 7, 2021, 9:05pm UTC](https://discourse.slicer.org/t/label-image-defects-after-saving/20062/1 "2021-10-07T21:05:20Z")

</div>

Guys, I am not sure if that is the correct place to ask that, BUT I have nowhere to run.

I used `nrrd` and `slicerio` extensions to separate `.seg.nrrd` files into separated segments or label images.  
Great. It gives me array images. I, then, proceed SAVING those labels into local image files (`.jpeg`). Good.

The problem arises when I load such images back into say jupyter notebook for Deep Learning application.

I created a binary, `1 and 0`, label image and saved in `.jpeg`, however when I load them back into python, some random pixels with value 2 appears. Yes! 2. How come?

I checked loading with both `matplotlib` `(imread)` and `Pillow`. By the way, I saved those images with Pillow in `.jpeg` format. I’ve tracked the whole code and everything sounds ok. The crashing happens after saving and loading it back.

I am working with deep learning semantic segmentation and it is crucial that the image have only 0 and 1`s.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [October 7, 2021, 9:23pm UTC](https://discourse.slicer.org/t/label-image-defects-after-saving/20062/2 "2021-10-07T21:23:12Z")

</div>

I’d guess those are compression artifacts from the jpg encoding. Maybe you can use .png instead?

---

<div class="post-metadata">

### Author: ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)
#### Post date: [October 7, 2021, 9:47pm UTC](https://discourse.slicer.org/t/label-image-defects-after-saving/20062/3 "2021-10-07T21:47:33Z")

</div>

The current recommendation for Medical Imaging Deep Learning is to use [Monai](https://github.com/Project-MONAI/MONAI) where you are using the medical imaging file directly such as a nrrd file. You are likely to run into issues using simple image formats like jpeg.

Tutorials for Monai are available at [GitHub - Project-MONAI/tutorials: MONAI Tutorials](https://github.com/Project-MONAI/tutorials).

---

<div class="post-metadata">

### Author: ![MachadoL](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/machadol/32/3372_2.png) [@MachadoL](https://discourse.slicer.org/u/MachadoL)
#### Post date: [October 8, 2021, 2:02pm UTC](https://discourse.slicer.org/t/label-image-defects-after-saving/20062/4 "2021-10-08T14:02:38Z")

</div>

That was it. Thanks a lot, @pieper!
