Converting a list of colors into an image

I want to manipulate colors, and merge the resulting vector and rasterize it into an image, controlling the resolution of the pixel.

E.g. I tried to:

But what I would like to have is an image of 3 square, blue, red and green, so that:

• I can control the size of the square (e.g. in pixels, say a square 32x32 each)

• (possibly!) I can have controls not to have the black perimeter line of the squares.

I can see there is an example to convert a matrix to graphics :

but cannot figure out how to achieve that with colors - e.g. by converting the color in values and than shape a matrix NxM..

Final thing, it will be exporting: I'd like to possibly determine the min size of square "pixel" in pixels:

How to convert a list of colors into arrays or matrixes, and then to an image, using above criteria?

• @m_goldberg - all answers are correct and I see you added a third one and even are editor. I marked as "correct" the first chronologically received. – user305883 Feb 17 '19 at 8:26

colors = RandomColor[{5, 5}]


Image[colors, ImageSize -> 1 -> 32]


If you a list of list of numbers instead of colors as input, you can use

list = colors /. RGBColor -> List;
Image[list, ImageSize -> 1 -> 32]


same picture

• Is the syntax ImageSize -> 1 -> 32 documented somewhere? – Alexey Popkov Feb 15 '19 at 7:59
• @AlexeyPopkov, afaik it is not documented. Earliest reference I could find on this site is this answer by rm-rf. See also this answer by CarlWoll – kglr Feb 15 '19 at 8:08

Your idea of using Rasterization can be made to work. You must take window magnification into account to get to proper size in pixels.

swatchToRaster[color_?ColorQ, size_ /; size > 0, mag_Real: 1.25] :=
Rasterize[color , ImageSize -> size/mag]

Row[Prepend[swatchToRaster[#, 32, 1.5] & /@ {Red, Green, Blue}, Spacer[50]]]


I used the ruler tool from the xTools app to prove that the size of rasterized swatches are indeed 32 x 32 pixels.

Note: when defining swatchToRaster, I gave the mag argument the default of 1.25 because I normally work with notebooks having magnification set to 125%. You should change this default if you normally use a different window magnification.

Each color is represented by a triplet of values {r,g,b} where each element is drawn from 0 to 1. You can get a matrix of these, turn it into an image with Image, and then resize:

Image[RandomReal[{0, 1}, {4, 5, 3}], ImageSize -> 1 -> 32]


• Interesting use of RandomReal[]. However I did not understood exactly how r,g,b triples are treated by Image[] - if I try Image[RandomReal[{0, 1}, {5, 6, 121}]], I see {5,6} are the matrix size, maybe 121 the number of channels? – user305883 Feb 17 '19 at 8:32
• The matrix sent to image should be {rows, col, 3} where 3 is the {r,g,b} value. I'm not sure what to expect if the third entry is larger than 3. – bill s Feb 17 '19 at 18:47