# Most idomatic way to convert an Images.jl Image to a 3-d array?

**URL:** <https://discourse.julialang.org/t/most-idomatic-way-to-convert-an-images-jl-image-to-a-3-d-array/7104>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 16, 2017, 5:02pm UTC](https://discourse.julialang.org/t/most-idomatic-way-to-convert-an-images-jl-image-to-a-3-d-array/7104 "2017-11-16T17:02:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mac01021](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mac01021/32/2078_2.png) [@mac01021](https://discourse.julialang.org/u/mac01021)\
**Post date:** [November 16, 2017, 5:02pm UTC](https://discourse.julialang.org/t/most-idomatic-way-to-convert-an-images-jl-image-to-a-3-d-array/7104/1 "2017-11-16T17:02:20Z")

</div>

I’ve loaded an image file to get a 2-d array of RGB color values. What I want is a 3-d array with depth 3.

The function below will do the job. But is there a more idomatic way?

```
rgb_to_array(img) = begin
    H, W = height(img), width(img)
    a = zeros(H, W, 3)
    for w in 1:W
        for h in 1:H
            rgb = img[h,w]
            a[h,w,1] = rgb.r
            a[h,w,2] = rgb.g
            a[h,w,3] = rgb.b
        end
    end
    a
end

```

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [November 16, 2017, 5:33pm UTC](https://discourse.julialang.org/t/most-idomatic-way-to-convert-an-images-jl-image-to-a-3-d-array/7104/2 "2017-11-16T17:33:55Z")

</div>

I think this should do the job:  
`permutedims(reinterpret(eltype(eltype(img)), img), (2, 3, 1))`

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 16, 2017, 5:35pm UTC](https://discourse.julialang.org/t/most-idomatic-way-to-convert-an-images-jl-image-to-a-3-d-array/7104/3 "2017-11-16T17:35:08Z")

</div>

Check out channelview(): [http://juliaimages.github.io/latest/quickstart.html#Colors,-the-0-to-1-intensity-scale,-and-views-1](http://juliaimages.github.io/latest/quickstart.html#Colors,-the-0-to-1-intensity-scale,-and-views-1)

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [November 16, 2017, 5:36pm UTC](https://discourse.julialang.org/t/most-idomatic-way-to-convert-an-images-jl-image-to-a-3-d-array/7104/4 "2017-11-16T17:36:42Z")

</div>

```julia
permutedims(channelview(img), (2,3,1))

```

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [November 16, 2017, 5:37pm UTC](https://discourse.julialang.org/t/most-idomatic-way-to-convert-an-images-jl-image-to-a-3-d-array/7104/5 "2017-11-16T17:37:51Z")

</div>

note that the `permutedims` is only needed because you seem to want the color channel to be in the last dimension.

---

<div class="post-metadata">

**Author:** ![mac01021](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mac01021/32/2078_2.png) [@mac01021](https://discourse.julialang.org/u/mac01021)\
**Post date:** [November 16, 2017, 6:25pm UTC](https://discourse.julialang.org/t/most-idomatic-way-to-convert-an-images-jl-image-to-a-3-d-array/7104/6 "2017-11-16T18:25:23Z")

</div>

Awesome. Thanks!

---

<div class="post-metadata">

**Author:** ![Yoshua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoshua/32/8872_2.png) [@Yoshua](https://discourse.julialang.org/u/Yoshua)\
**Post date:** [July 23, 2019, 11:03pm UTC](https://discourse.julialang.org/t/most-idomatic-way-to-convert-an-images-jl-image-to-a-3-d-array/7104/7 "2019-07-23T23:03:54Z")

</div>

Hi! i have problem manipulating 3-D image.  
I want to save a 3-D array of image (m, n, 3) in png format. I tried some functions and they gave me errors.

Thank you in advance who can help.
