# How to convert a matrix to an RGB image using Images.jl?

**URL:** <https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265>\
**Category:** General Usage\
**Tags:** images\
**Created:** [November 23, 2017, 11:10am UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265 "2017-11-23T11:10:49Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![robblackwell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robblackwell/32/2720_2.png) [@robblackwell](https://discourse.julialang.org/u/robblackwell)\
**Post date:** [November 23, 2017, 11:10am UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/1 "2017-11-23T11:10:49Z")

</div>

Let’s say that I have a matrix x

```julia
x = [0 0.5 1; 0.6 0.7 08.; 1 0.1 0.2]

```

I can display it as a grayscale image using

```julia
Gray.(x)

```

How can I convert it to an RGB image using a specified color gradient?

Plots.jl allows me to do something similar with

```julia
Plots.heatmap(x, yflip=true, color=:inferno)

```

But I’d like the actual image rather than the plot of the image please.

---

<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:** [November 23, 2017, 3:01pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/2 "2017-11-23T15:01:15Z")

</div>

What do you mean by " actual image rather than the plot of the image"? An image file on disk or the image array at the REPL?

You can get both with GMT. For example this

```julia
imshow(x, cmap="rainbow", fmt="png")

```

creates a _GMTjl\_tmp.png_ file in you temporary dir and displays it (you need ghostscript installed too).

---

<div class="post-metadata">

**Author:** ![robblackwell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robblackwell/32/2720_2.png) [@robblackwell](https://discourse.julialang.org/u/robblackwell)\
**Post date:** [November 23, 2017, 4:07pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/3 "2017-11-23T16:07:17Z")

</div>

I’d like an RGB image array (as per Images.jl) to be returned and displayed in IJulia.

The following works, but gives me a color image where ll the colours are gray

```julia
RGB.(x, x, x)

```

Ideally, I want to say something like

```julia
RGB(x, cmap=:inferno)

```

so that the colors are interpolated from a color gradient.

Thanks for your input, but I don’t really want to use ImageView (I’m in IJulia) and anyway, your example doesn’t work for me.

---

<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:** [November 23, 2017, 4:18pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/4 "2017-11-23T16:18:02Z")

</div>

There is no ImageView involved in what I said. But you would need [GMT](https://github.com/GenericMappingTools/GMT.jl) and Ghostscript installed and that `imshow` is from GMT. To have it displayed on IJulia you would need something like

```julia
GMT.imshow(x, cmap="rainbow", fmt="png", show=false)

```

and than load the the /tmp/GMTjl\_tmp.png (or the equivalent on Windows) from within IJulia.

---

<div class="post-metadata">

**Author:** ![Ken-B](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken-b/32/1780_2.png) [@Ken-B](https://discourse.julialang.org/u/Ken-B)\
**Post date:** [November 23, 2017, 5:44pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/5 "2017-11-23T17:44:37Z")

</div>

You could use the `applycolormap` from the `PerceptualColourMaps` package, e.g.:

```julia
using TestImages
img = testimage("moonsurface") # a grayscale image
using PerceptualColourMaps
imgc = applycolormap(img, cmap("R3")) # outputs a 3-dimensional array
using Images
imgc2 = colorview(RGB, permuteddimsview(imgc, (3,1,2)))

```

which plots in my terminal using the nice `TerminalExtensions` package 🙂  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/e/8e3ef7f2de3f3e699a8aa35dcbd1e68d62fb2c99.jpeg)

Cheers!

---

<div class="post-metadata">

**Author:** ![robblackwell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robblackwell/32/2720_2.png) [@robblackwell](https://discourse.julialang.org/u/robblackwell)\
**Post date:** [November 23, 2017, 6:22pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/6 "2017-11-23T18:22:18Z")

</div>

Wow! -that looks exactly what I want - thanks.

Unfortunately on my system (Linux) with julia version 0.6.1 I get

```julia
using PerceptualColourMaps

ERROR: LoadError: LoadError: UndefVarError: AbstractImage not defined
Stacktrace:

```

and

```julia
cmap("R3")

ERROR: UndefVarError: bbspline not defined

```

Perhaps I need to clone the bleeding edge stuff from Github? Or do I need Julia 0.7.x ?

---

<div class="post-metadata">

**Author:** ![robblackwell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robblackwell/32/2720_2.png) [@robblackwell](https://discourse.julialang.org/u/robblackwell)\
**Post date:** [November 23, 2017, 6:26pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/7 "2017-11-23T18:26:27Z")

</div>

Yes

```julia
Pkg.clone("https://github.com/peterkovesi/PerceptualColourMaps.jl")

```

and it works! many thanks. RB

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [November 23, 2017, 6:40pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/8 "2017-11-23T18:40:39Z")

</div>

@robblackwell consider using Plots.jl if your goal is only plotting a 2D matrix with a custom colormap.

```julia
using Plots

heatmap(img, cmap=:inferno)

```

---

<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 26, 2017, 10:06am UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/9 "2017-11-26T10:06:04Z")

</div>

These are all great suggestions. Just for the record, one other alternative is to use [IndirectArrays](https://github.com/JuliaArrays/IndirectArrays.jl), which can be used to [lazily](https://en.wikipedia.org/wiki/Lazy_evaluation) construct a colormap array. In this case you’d need to convert your `Float64` array to an `Int` array, which you could do in a lazy way with [MappedArrays](https://github.com/JuliaArrays/MappedArrays.jl). For example:

```julia
using MappedArrays, IndirectArrays

function color_me(A, cmap)
    n = length(cmap)
    f = s->clamp(round(Int, (n-1)*s)+1, 1, n) # safely convert 0-1 to 1:n
    Ai = mappedarray(f, A) # like f.(A) but does not allocate significant memory
    IndirectArray(Ai, cmap) # colormap array
end

```

(For this to work you have to be running the very latest `IndirectArrays`.)

In most cases you may find PerceptualColourmap’s `applycolormap` easier, since it’s already written. Cases there might be reasons to consider `IndirectArrays`:

- big data (e.g., \>1TB image): the version here allocates essentially no memory and takes essentially no time (you pay only when you access the values, and then only for those values you need)
- in applications where you’d like to pass the colorized array to a function but also have access to the original values (which you can get with `parent(imgc.index)`). An example might be a GUI “tooltip hover” application.

Again, these probably don’t matter in most cases. Not only is `applycolormap` already available, but @peterkovesi’s color maps are so well designed that you should just be using those anyway 🙂.

---

<div class="post-metadata">

**Author:** ![robblackwell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robblackwell/32/2720_2.png) [@robblackwell](https://discourse.julialang.org/u/robblackwell)\
**Post date:** [November 27, 2017, 9:27am UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/10 "2017-11-27T09:27:46Z")

</div>

Interesting. Thanks for taking the time to respond and for all the great work!

---

<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:** [January 17, 2021, 7:42pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/11 "2021-01-17T19:42:21Z")

</div>

Sorry to resurrect this old thread but Prof. Edelman’s recent Julia [video](https://youtu.be/ConoBmjlivs) about Structure _(Structure | Week3 | 18.S191 MIT Fall 2020)_ led me here.

A function `show_image()` is used in a Pluto notebook to display matrices as images in color, instead of using `heatmap()`. Could not find this function anywhere, until finding @tim.holy’s `color_me()` in post above.

In case it might be useful to others, please find herein a working example (at least on Win10 Julia 1.5.3 VS\_Code):

```julia
using Plots, MappedArrays, IndirectArrays, PerceptualColourMaps

outer(v,w) = [x * y for x ∈ v, y ∈ w] # Prof. Edelman's: https://youtu.be/ConoBmjlivs
show_image(m) = plot(color_me(m,cmap("R3"))) # color_me() by @tim.holy

m = outer(rand(8),rand(12))
show_image(m)

```

![Multiplication_table_of_randoms](https://global.discourse-cdn.com/julialang/original/3X/e/1/e1be3489458fa93edfb572630e1599cb8c3ace3d.png)

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [June 19, 2021, 1:11pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/12 "2021-06-19T13:11:06Z")

</div>

After 6 months I also resurect this thread, because I’d like to use a colorscheme defined in ColorSchemes.jl, instead of a cmap from PerceptualColourMaps. I didn’t succeed with a colorscheme. How to access a colorscheme and pass it as an argument for `color_me`? Thanks!!

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [June 19, 2021, 6:03pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/13 "2021-06-19T18:03:15Z")

</div>

Meanwhile I succeeded to use a colorscheme as folows:

```julia
color_me(m, ColorSchemes.balance.colors)

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 9, 2021, 9:29am UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/14 "2021-07-09T09:29:54Z")

</div>

Here is another version of the function that scales the colormap linearly between the extrema of `A`:

```julia
    function color_me_scaleminmax(A, cmap)
        n = length(cmap)
        scale = takemap(scaleminmax, A)
        f = s->clamp(round(Int, (n-1)*scale(s))+1, 1, n) # safely convert 0-1 to 1:n
        Ai = mappedarray(f, A) # like f.(A) but does not allocate significant memory
        IndirectArray(Ai, cmap) # colormap array
    end

    precompile(color_me_scaleminmax, (Matrix{Float64}, Vector{RGB{Float64}}))

```

---

<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:** [July 11, 2021, 10:25pm UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/15 "2021-07-11T22:25:19Z")

</div>

@mkitti, could you confirm that `takemap` comes from using `ImageCore.jl`?

_ **NB:** and also comment on the use of_ `precompile`?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 12, 2021, 5:20am UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/16 "2021-07-12T05:20:32Z")

</div>

[takemap is from ImageCore.jl](https://juliaimages.org/ImageCore.jl/stable/reference/#ImageCore.takemap). Folow the link.

I added the precompile statement to indicate what types I used with this function. It is also extracted from this code here:

> <https://github.com/mkitti/GaussianBeamLens/blob/c49230287c15cc93c60dc93ad3e588f03d98fbbd/src/GtkGUI.jl#L19-L25>

(edit: I forgot to push and update the code)

If this piece of code is that popular, perhaps we should package it.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [November 5, 2021, 9:31am UTC](https://discourse.julialang.org/t/how-to-convert-a-matrix-to-an-rgb-image-using-images-jl/7265/17 "2021-11-05T09:31:33Z")

</div>

Why use both IndirectArray and mappedarray here? Wouldn’t the code below work as well?

```julia
function color_me(A, clr_map)
    n = length(clr_map)
    f(s) = clr_map[clamp(round(Int, (n-1)*s)+1, 1, n)]
    Am = mappedarray(f, A)
    return of_eltype(eltype(clr_map), Am)
end

```

I guess it’s connected with [this discussion](https://github.com/JuliaArrays/GetindexArrays.jl/issues/3#issuecomment-932774743), that is, MappedArrays are powerful.
