# Read labeled image with Images.jl

**URL:** <https://discourse.julialang.org/t/read-labeled-image-with-images-jl/70934>\
**Category:** General Usage\
**Tags:** images\
**Created:** [November 4, 2021, 6:40am UTC](https://discourse.julialang.org/t/read-labeled-image-with-images-jl/70934 "2021-11-04T06:40:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsobako](https://avatars.discourse-cdn.com/v4/letter/t/a698b9/32.png) [@tsobako](https://discourse.julialang.org/u/tsobako)\
**Post date:** [November 4, 2021, 6:40am UTC](https://discourse.julialang.org/t/read-labeled-image-with-images-jl/70934/1 "2021-11-04T06:40:28Z")

</div>

Hi.  
I have made segmentation labeling of images with the following tool ([Labelme repo](https://github.com/wkentaro/labelme/)). It allows to export labels as a PNG image with every pixel as a single byte and it could be possible to load it properly with Python’s PIL and NumPy libraries ([https://github.com/wkentaro/labelme/tree/main/examples/tutorial#how-to-load-label-png-file](https://github.com/wkentaro/labelme/tree/main/examples/tutorial#how-to-load-label-png-file)).

I’ve tried to load these PNG images with the `load` function but it loads the image as a matrix of RGB pixels.  
What parameters could be passed to the `load` function to get the image loaded as a matrix of bytes?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 4, 2021, 9:23am UTC](https://discourse.julialang.org/t/read-labeled-image-with-images-jl/70934/2 "2021-11-04T09:23:01Z")

</div>

Hello and welcome.  
Have you tried `Images.jl` function: `rawview(image)`

---

<div class="post-metadata">

**Author:** ![trilobit](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@trilobit](https://discourse.julialang.org/u/trilobit)\
**Post date:** [November 4, 2021, 11:14am UTC](https://discourse.julialang.org/t/read-labeled-image-with-images-jl/70934/3 "2021-11-04T11:14:46Z")

</div>

Untested, but RGB seems to be the means of choice.

 ![img](https://global.discourse-cdn.com/julialang/original/3X/1/0/10f007c59a9a384cd6e86b3b06a5cb291ceffab0.webp)

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 4, 2021, 12:59pm UTC](https://discourse.julialang.org/t/read-labeled-image-with-images-jl/70934/4 "2021-11-04T12:59:39Z")

</div>

If we’re loading it as an RGB, it’s presumably a problem with `Labelme`: it should be saving the images as grayscale, not RGB.

Our image-loading libraries would load a single-channel, single-byte image as `Gray{N0f8}`, but you can get back to integers with `rawview(channelview(img))`.

---

<div class="post-metadata">

**Author:** ![tsobako](https://avatars.discourse-cdn.com/v4/letter/t/a698b9/32.png) [@tsobako](https://discourse.julialang.org/u/tsobako)\
**Post date:** [November 4, 2021, 2:12pm UTC](https://discourse.julialang.org/t/read-labeled-image-with-images-jl/70934/5 "2021-11-04T14:12:37Z")

</div>

Unfortunately, `rawview` is not working.

Also, I analyzed the image and found that they used 8bit format, but PNG is paletted - so if there any function in `Images` that allow to read palette from PNG file?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 5, 2021, 7:02am UTC](https://discourse.julialang.org/t/read-labeled-image-with-images-jl/70934/6 "2021-11-05T07:02:14Z")

</div>

Since it’s an `RGB`, try `rawview(channelview(Gray.(img)))`.

See the documentation at [Home · JuliaImages](https://juliaimages.org/latest/) if you’re still confused.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 5, 2021, 10:01am UTC](https://discourse.julialang.org/t/read-labeled-image-with-images-jl/70934/7 "2021-11-05T10:01:50Z")

</div>

Also, yes, PNGFiles (which you get if ImageIO is handling your I/O) should be able to handle paletted files. [https://github.com/JuliaIO/PNGFiles.jl/blob/b32b97f039f68172fe75c6084c34fa0240689a52/src/io.jl#L18-L20](https://github.com/JuliaIO/PNGFiles.jl/blob/b32b97f039f68172fe75c6084c34fa0240689a52/src/io.jl#L18-L20)

---

<div class="post-metadata">

**Author:** ![tsobako](https://avatars.discourse-cdn.com/v4/letter/t/a698b9/32.png) [@tsobako](https://discourse.julialang.org/u/tsobako)\
**Post date:** [November 5, 2021, 2:11pm UTC](https://discourse.julialang.org/t/read-labeled-image-with-images-jl/70934/8 "2021-11-05T14:11:13Z")

</div>

Thanks, @tim.holy .  
Usage of PNGFiles helps me.

Example of solution

```julia
using PNGFiles
img = PNGFiles.load("label.png"; expand_paletted=true) # expand_paletted=true is crucial
# img.index - contains matrix with labels
# unique(img.index) - contains all label values

```
