# Save Matrix of RGB Values to Image File with Huge Pixels

**URL:** <https://discourse.julialang.org/t/save-matrix-of-rgb-values-to-image-file-with-huge-pixels/47771>\
**Category:** New to Julia\
**Tags:** plotting\
**Created:** [October 5, 2020, 7:56am UTC](https://discourse.julialang.org/t/save-matrix-of-rgb-values-to-image-file-with-huge-pixels/47771 "2020-10-05T07:56:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pascal-mueller](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@pascal-mueller](https://discourse.julialang.org/u/pascal-mueller)\
**Post date:** [October 5, 2020, 7:56am UTC](https://discourse.julialang.org/t/save-matrix-of-rgb-values-to-image-file-with-huge-pixels/47771/1 "2020-10-05T07:56:00Z")

</div>

Hi,

so yesterday I tried to visualize a graph in the form of a matrix. A lot of people helped me in the forum and the chat. I ended up with turning the numerical values in my matrix to RGB values. So now I have e.g.:

```julia
using FileIO
using Images

c = RGB{Normed{UInt8,8}}[
        RGB{N0f8}(1.0,1.0,1.0) RGB{N0f8}(0.0,0.502,0.0) RGB{N0f8}(1.0,0.0,0.0);
        RGB{N0f8}(1.0,0.0,0.0) RGB{N0f8}(1.0,1.0,1.0) RGB{N0f8}(0.0,0.0,0.0);
        RGB{N0f8}(0.0,0.502,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.502,0.0)]

save("image.png", c) # For jupyter use FileIO.save("image.png", c)

```

Problem: I get an image of 3x3 pixels whereas is want a picture like  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/5/051219b5f97324e9637ee40d459d3386c74cf896.png)

If anyone could help me figure out how to make my pixels huge, I’d be in love forever.The best szenario would be to define an image size nd then just have it scale automatically becuse the size of the matrix will change. 🙂

---

<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:** [October 5, 2020, 12:02pm UTC](https://discourse.julialang.org/t/save-matrix-of-rgb-values-to-image-file-with-huge-pixels/47771/2 "2020-10-05T12:02:03Z")

</div>

With GMT

```julia
using GMT
im = ones(UInt8,2, 2, 3); im[1,1,1]=255; im[1,2,1]=255; im[1,1,2]=255; im[2,1,2]=255; im[1,1,3]=255; im[2,2,3]=255;

imshow(im,axes=:none, figsize=10, savefig="huge_pix.png")

```

What happens under the hood is:

- a postscript file with exactly 10x10 cm is created
- the postscript file is rasterized into a png witha certain desinsity that I don’t remember (would need to dive into the docs).

If you print the `ps` (or a converted to pdf) file you would get a printed image with exactly 10 cm.

 ![huge_pix](https://global.discourse-cdn.com/julialang/original/3X/a/9/a9b9b40dcf7c901c877fe94796d90620150df140.png)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 5, 2020, 12:39pm UTC](https://discourse.julialang.org/t/save-matrix-of-rgb-values-to-image-file-with-huge-pixels/47771/3 "2020-10-05T12:39:54Z")

</div>

This is in PGFPlotsX.jl. It is slightly more awkward than ideal:

```julia
using Images, PGFPlotsX

cs = RGB{Normed{UInt8,8}}[
               RGB{N0f8}(1.0,1.0,1.0) RGB{N0f8}(0.0,0.502,0.0) RGB{N0f8}(1.0,0.0,0.0);
               RGB{N0f8}(1.0,0.0,0.0) RGB{N0f8}(1.0,1.0,1.0) RGB{N0f8}(0.0,0.0,0.0);
               RGB{N0f8}(0.0,0.502,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.502,0.0)]
x = vec(first.(Tuple.(CartesianIndices(cs))))
y = vec(last.(Tuple.(CartesianIndices(cs))))

c = Coordinates(
    x, y
    ; meta = vec(cs))

p = @pgf PGFPlotsX.Axis(
    {
        axis_equal,
        ticks="none",
    },
    PlotInc(
        {
            mark = "none",
            matrix_plot,
            "mesh/color input" = "explicit",
            "mesh/cols" = 3
        },
        c,
    )
);

pgfsave("image.pdf", p)

```

 ![Screenshot 2020-10-05 at 14.37.15](https://global.discourse-cdn.com/julialang/original/3X/f/b/fba3aa9112b9a723dc8706f9d24bf8acb9506094.png)

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [October 5, 2020, 1:22pm UTC](https://discourse.julialang.org/t/save-matrix-of-rgb-values-to-image-file-with-huge-pixels/47771/4 "2020-10-05T13:22:03Z")

</div>

You should be able to do this natively from Images.jl by specifying the interpolation type for `imresize`, but there’s a longstanding issue: [https://github.com/JuliaImages/ImageTransformations.jl/issues/51](https://github.com/JuliaImages/ImageTransformations.jl/issues/51). It’s not a complicated fix, but nobody’s taken the time to do it yet.

---

<div class="post-metadata">

**Author:** ![ianshmean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ianshmean/32/216042_2.png) [@ianshmean](https://discourse.julialang.org/u/ianshmean)\
**Post date:** [October 5, 2020, 1:54pm UTC](https://discourse.julialang.org/t/save-matrix-of-rgb-values-to-image-file-with-huge-pixels/47771/5 "2020-10-05T13:54:38Z")

</div>

The easiest would probably be to just inner `repeat` your array

```julia
scale = 100 # times bigger you want the image
save("image.png", repeat(c, inner = (scale, scale))) 

```

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [October 5, 2020, 6:25pm UTC](https://discourse.julialang.org/t/save-matrix-of-rgb-values-to-image-file-with-huge-pixels/47771/6 "2020-10-05T18:25:38Z")

</div>

In [Gaston.jl](https://github.com/mbaz/Gaston.jl):

```julia
using Images, Gaston

c = RGB{Normed{UInt8,8}}[
               RGB{N0f8}(1.0,1.0,1.0) RGB{N0f8}(0.0,0.502,0.0) RGB{N0f8}(1.0,0.0,0.0);
               RGB{N0f8}(1.0,0.0,0.0) RGB{N0f8}(1.0,1.0,1.0) RGB{N0f8}(0.0,0.0,0.0);
               RGB{N0f8}(0.0,0.502,0.0) RGB{N0f8}(0.0,0.0,0.0) RGB{N0f8}(0.0,0.502,0.0)];
cc = channelview(c).*255; # convert to a 3-D array of RGB values
imagesc(cc,Axes(size="square",tics=:off))

```

![image](https://global.discourse-cdn.com/julialang/original/3X/8/e/8ea2c3d08ca9f2912946b17de0e427d451b2e37b.png)

---

<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:** [October 5, 2020, 9:18pm UTC](https://discourse.julialang.org/t/save-matrix-of-rgb-values-to-image-file-with-huge-pixels/47771/7 "2020-10-05T21:18:59Z")

</div>

The proportions are distorted when I run the same GMT code in Windows 10 (GMT6.1.1):

 ![huge_pix](https://global.discourse-cdn.com/julialang/original/3X/8/0/80ae85dd793fc0699c7f2d0798829a8ff5cc1f20.png)

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 5, 2020, 9:58pm UTC](https://discourse.julialang.org/t/save-matrix-of-rgb-values-to-image-file-with-huge-pixels/47771/8 "2020-10-05T21:58:51Z")

</div>

Following up on yesterday’s discussion, here is what I do in Jupyterlab:

```julia
using Plots
#... like yesterday
function num2col(i)
    if i == 0
        return colorant"white"
    elseif i == 1
        return colorant"green"
    elseif i == 2
        return colorant"red"
    else
        return colorant"black"
    end
end
#
mat = [0 3; 2 1]
#
plot(num2col.(mat),xaxis=nothing,yaxis=nothing)

```

which produces the following in Jupyterlab:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/f/7ff5ebfa422de243cdaaad3e081804a37e9ab84e.png)  
Then I save the result as, e.g., an \*.svg file:

```julia
savefig("c:/users/.../squares.svg")

```

I then import this file in, say, the LyX word processor:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/d/ddc2527cc7334247be04c2c5a02dc13023ce5428.png)

If I do a pdf preview of this, I get:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/4/14d5058de0a27953122896805e614c1a8fef7c11.png)

If I import it into Word, I get:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/1/318086817acc26005d23026c818f528f3eea8e3c.png)

I suspect the trick is to _plot_ the matrix, and then use _savefig_ – instead of just saving the matrix??

To adjust the size of the plot, you can use the `size` keyword of the `plot` function. You can, of course, also scale the svg file (or pdf or png) in your word processor.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [October 5, 2020, 10:21pm UTC](https://discourse.julialang.org/t/save-matrix-of-rgb-values-to-image-file-with-huge-pixels/47771/9 "2020-10-05T22:21:05Z")

</div>

Btw: the following is a better version of my `num2col` function (still assuming that the numeric values are integers in the range {0,1,2,…}):

```julia
function num2col(i;vcol = [colorant"red",colorant"green",colorant"blue"])
   return vcol[min(i+1,length(vcol))]
end

```

Then you find:

```julia
m = rand(0:3,2,4)
num2col.(m)

```

gives, say:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/0/d0f4c33356eb087215e44e277a52e3312d61f2a9.png)

while specifying some colors like:

```julia
MYCOL = distinguisable_colors(4,colorant"red")
#
num2col.(m;vcol=MYCOL)

```

leads to

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/5/9584bc9ee955317b4e37e98434b7f79b6599f09f.png)

Here, function `distinguishable_colors()` gives maximally distinguishable colors where the first argument gives the number of colors, and the optionally second argument gives a “seed” for picking the colors. (The seed can be a “scalar” color, or a vector of colors, I think.)

---

<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:** [October 5, 2020, 10:42pm UTC](https://discourse.julialang.org/t/save-matrix-of-rgb-values-to-image-file-with-huge-pixels/47771/10 "2020-10-05T22:42:51Z")

</div>

Works correctly for me in my Win10 and WSL machines but can reproduce this issue in a CentosOS machine.  
Have an idea where it might coming from but puzzled with this machines discrepancy. Better move this ito a GMT.jl issue

---

<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:** [October 19, 2020, 5:23pm UTC](https://discourse.julialang.org/t/save-matrix-of-rgb-values-to-image-file-with-huge-pixels/47771/11 "2020-10-19T17:23:09Z")

</div>

For the record, this issue has been fixed in the GMT master and will be part of next official release. The compiled Windows installer (GMT 6.2.0dev) provided by joa-quim does not show the bug:

 ![huge_pix](https://global.discourse-cdn.com/julialang/original/3X/a/9/a9b9b40dcf7c901c877fe94796d90620150df140.png)
