4
$\begingroup$

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:

enter image description here

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 :

Image[(

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:

enter image description here

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

$\endgroup$
1
  • $\begingroup$ @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. $\endgroup$
    – user305883
    Feb 17, 2019 at 8:26

3 Answers 3

7
$\begingroup$
colors = RandomColor[{5, 5}]

enter image description here

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

enter image description here

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

$\endgroup$
2
  • $\begingroup$ Is the syntax ImageSize -> 1 -> 32 documented somewhere? $\endgroup$ Feb 15, 2019 at 7:59
  • 1
    $\begingroup$ @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 $\endgroup$
    – kglr
    Feb 15, 2019 at 8:08
3
$\begingroup$

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]]]

colors

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

measured

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.

$\endgroup$
2
$\begingroup$

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]

enter image description here

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – user305883
    Feb 17, 2019 at 8:32
  • $\begingroup$ 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. $\endgroup$
    – bill s
    Feb 17, 2019 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.