# How to prepare data for using in Images

**URL:** <https://discourse.julialang.org/t/how-to-prepare-data-for-using-in-images/2830>\
**Category:** General Usage\
**Tags:** question, package, images\
**Created:** [March 22, 2017, 11:35pm UTC](https://discourse.julialang.org/t/how-to-prepare-data-for-using-in-images/2830 "2017-03-22T23:35:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [March 22, 2017, 11:35pm UTC](https://discourse.julialang.org/t/how-to-prepare-data-for-using-in-images/2830/1 "2017-03-22T23:35:00Z")

</div>

With [GMT.jl](https://github.com/joa-quim/GMT.jl) we can read or produce high quality images (like the one [here](https://github.com/joa-quim/GMT.jl/blob/master/WL_Example_III.ipynb)) but the output is a MxNx2 uint8 array. And I would like to display them with Images.jl but can’t find a way to reformat the data.

I did try things around this

```julia
using FixedPointNumbers, Colors, ColorTypes
z=zeros(UInt8,2,2,3);
z[1,1,1] = 255; z[1,2,2] = 255; z[2,1,3] = 255;
imf8 = reinterpret(N0f8, z)
2×2×3 Array{FixedPointNumbers.Normed{UInt8,8},3}:
[:, :, 1] =
 1.0N0f8 0.0N0f8
 0.0N0f8 0.0N0f8

[:, :, 2] =
 0.0N0f8 1.0N0f8
 0.0N0f8 0.0N0f8

[:, :, 3] =
 0.0N0f8 0.0N0f8
 1.0N0f8 0.0N0f8

RGB24.(reshape(permutedims(imf8,[3 2 1])[:],2,6))
2×6 Array{ColorTypes.RGB24,2}:
 RGB24{N0f8}(1.0,1.0,1.0) RGB24{N0f8}(0.0,0.0,0.0) RGB24{N0f8}(1.0,1.0,1.0) … RGB24{N0f8}(1.0,1.0,1.0) RGB24{N0f8}(0.0,0.0,0.0)
 RGB24{N0f8}(0.0,0.0,0.0) RGB24{N0f8}(0.0,0.0,0.0) RGB24{N0f8}(0.0,0.0,0.0) RGB24{N0f8}(0.0,0.0,0.0) RGB24{N0f8}(0.0,0.0,0.0)

```

but that’s not what it takes to create a color image (a 4 pixels Red,Green,Blue,Black image in this case).

How can I do it? This problem is clearly solved by the `FileIO` `load()`` function but I can’t find the code that loads an image from file and reformats the data.

Thanks.

---

<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:** [March 23, 2017, 7:26am UTC](https://discourse.julialang.org/t/how-to-prepare-data-for-using-in-images/2830/2 "2017-03-23T07:26:01Z")

</div>

Give `ImageCore` a try. I am not quite sure what color type you are looking for since you talk about 3 different bit-depths one way or the other, but play around with the following code and see if it gives you what you are looking for

```julia
julia> using ImageCore, ColorTypes

julia> z=zeros(UInt8,2,2,3);

julia> z[1,1,1] = 255; z[1,2,2] = 255; z[2,1,3] = 255;

julia> imf8 = normedview(z)
2×2×3 Array{N0f8,3}:
[:, :, 1] =
 1.0N0f8 0.0N0f8
 0.0N0f8 0.0N0f8

[:, :, 2] =
 0.0N0f8 1.0N0f8
 0.0N0f8 0.0N0f8

[:, :, 3] =
 0.0N0f8 0.0N0f8
 1.0N0f8 0.0N0f8

julia> colorview(RGB, permutedims(imf8,[3 2 1]))
2×2 Array{RGB{N0f8},2}:
 RGB{N0f8}(1.0,0.0,0.0) RGB{N0f8}(0.0,0.0,1.0)
 RGB{N0f8}(0.0,1.0,0.0) RGB{N0f8}(0.0,0.0,0.0)

```

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [March 23, 2017, 2:10pm UTC](https://discourse.julialang.org/t/how-to-prepare-data-for-using-in-images/2830/3 "2017-03-23T14:10:05Z")

</div>

Thanks, that’s what I was looking for. I see, however, that when data is RGB Band Interleaved to use it in Images one have to do a data copy (here materialized by the permutedims() call).  
I wonder if this is inherent to all FileIO imports for formats that do not have the image stored as Pixel Interleaved.

---

<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:** [March 23, 2017, 5:19pm UTC](https://discourse.julialang.org/t/how-to-prepare-data-for-using-in-images/2830/4 "2017-03-23T17:19:33Z")

</div>

> [@joa-quim](#):
>
> when data is RGB Band Interleaved to use it in Images one have to do a data copy (here materialized by the permutedims() call).

Not quite. there is `permuteddimsview` in `ImageCore` which does not copy. You do sacrifice memory locality however.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [March 23, 2017, 5:26pm UTC](https://discourse.julialang.org/t/how-to-prepare-data-for-using-in-images/2830/5 "2017-03-23T17:26:41Z")

</div>

Ah, nice but loosing memory locality is probably worst than a copy. Anyway, this should not be an issue for my intents because I’m supposed to be able to control the memory layout of the array that comes out from GMT so no call to permutedims should be needed. ‘Just’ have to work it out now.
