Cubify This! A lesson in grayscale... er... color... er... whatever

Imagemagick (108)

Version: ImageMagick 6.8.7-7 Q16 x86_64 2013-11-27

The following call:

$ convert -resize 75x75 -fx "q=p.intensity;q<1/6?#0051BA:q<2/6?#C41E3A:q<3/6?#009e60:q<4/6?#ff5800:q<5/6?#FFD500:#FFF" input output

where input and output have to be modified for the input and output filename.

I only counted the chars between -resize and #FFF", if you think this is invalid feel free to comment.

I used Lenna as image (she appeared in a Playboy, and anyone doing that should count as a Hollywood celebrity, right?)

Input:

Input image

Output:

$ convert -resize 75x75 -fx "q=p.intensity;q<1/6?#0051BA:q<2/6?#C41E3A:q<3/6?#009e60:q<4/6?#ff5800:q<5/6?#FFD500:#FFF" Lenna.png LennaRubik.png

Generated image

Output enlarged:

Enlarged image

Notes: according to the imagemagick docs you cannot have more than one conditional operator in a statement, but the call still seems to work fine, so probably this was fixed and the docs were just not updated.

Running identify on the result image (to show the colors are indeed fine):

$ identify -verbose LennaRubik.png
  (...)   
  Colors: 6
  Histogram:
       338: (  0, 81,186) #0051BA srgb(0,81,186)
      1652: (  0,158, 96) #009E60 srgb(0,158,96)
      1187: (196, 30, 58) #C41E3A srgb(196,30,58)
      1674: (255, 88,  0) #FF5800 srgb(255,88,0)
       706: (255,213,  0) #FFD500 srgb(255,213,0)
        68: (255,255,255) #FFFFFF white
  (...)

If you think Lenna doesn't count as a proper celebrity, here is Bruce Willis:

Bruce Original

Bruce Small

Bruce Large


Mathematica

We'll work with a square region from a U.S. stamp featuring Greta Garbo. It will be referred to as j.

j

f[i_,rs_,m_:True]:=
Module[{(*rs=rastersize-4*)r={0.77,0.12,0.23},gr={0,0.62,0.38},b={0,0.32,0.73},o={1,0.35,0},y={1,0.84,0},w={1,1,1},
c1={r,gr,b,o,y,w},grayGreta,garboColors},
grayGreta=(*Reverse@*)ImageData[ColorQuantize[Rasterize[i,(*ImageResolution \[Rule]15*)RasterSize->rs+1,ImageSize->rs],6]];
garboColors=Union@Flatten[grayGreta,1];
ArrayPlot[grayGreta/.Thread[garboColors-> RGBColor@@@c1],
Mesh->If[m==True,{rs-1,rs-1},None],MeshStyle->Black]]

The function f takes 3 parameters:

  • i which refers to the image
  • rs, the raster size
  • m, a boolean variable that states whether Mesh lines should be used. (The default setting is True).

Using raster sizes of 15, 30, 45, and 75:

GraphicsGrid[{{f[j, 15], f[j, 30]}, {f[j, 45], f[j, 75]}}, ImageSize -> 800]

4 garbos

I can't imagine anyone making a Rubrik's cube with so many pieces! Interesting exercise nonetheless.


Playing around with colors

This is from an earlier entry. The code is slightly different. Graphics is used instead of ArrayPlot. Also, we use the full stamp even though it is not square.

There are 6!=720 permutations of the Rubrik cube colors.

The following displays the center image of the upper row (set 6 images below). When the grayscale values are arranged from darkest to lightest, the colors are {r,gr,b,o,y,w}. Other variations nonetheless work.

i is the original image in grayscale.

Graphics[Raster[(g=Reverse@ImageData[ColorQuantize[Rasterize[i,RasterSize->75],6]])
/.Thread[Union@Flatten[g,1]-> {{7,1,2},{0,6,4},{0,3,7},{10,4,0},{10,8,0},{10,10,10}}/10]]]

i is the original grayscale image of the full Greta Garbo stamp.

Rasterize[garbo,RasterSize->75 rasterizes the image into a 75 by 75 array.

ColorQuantize[<>, 6] reduces the grayscale values to a set of 6.

ImageData retrieves the data array from the image; it comes upside-down.

Reverse flips the data array, hence the picture, right-side up.

garboColors are the 6 grayscale values in the quantized image.

Graphics[Raster displays the final image.

rubrikColors are RGB values of the 6 Rubrik cube colors.

Various color permutations of Red, Green, Blue, Orange, Yellow, and White are given.

r={0.77,0.12,0.23};gr={0,0.62,0.38};b={0,0.32,0.73};o={1,0.35,0};y={1,0.84,0};w={1,1,1};
c1={r,gr,b,o,y,w};
c2={r,b,gr,o,y,w};
c3={b,r,gr,o,y,w};
c4={gr,b,r,o,y,w};
c5={b,r,gr,y,o,w};

And the code:

grayGreta=Reverse@ImageData[ColorQuantize[Rasterize[i,RasterSize->75],6]];
garboColors=Union@Flatten[grayGreta,1];
Grid[{{i,
Graphics[Raster[grayGreta/.Thread[garboColors-> c1]]],
Graphics[Raster[grayGreta/.Thread[garboColors-> c2]]]},
{Graphics[Raster[grayGreta/.Thread[garboColors-> c3]]],
Graphics[Raster[grayGreta/.Thread[garboColors-> c4]]],
Graphics[Raster[grayGreta/.Thread[garboColors-> c5]]]}}]

garbos


Garbos Galore

Here are 72 (of the 720) images of Greta Garbo that use the 6 Rubrik cube colors. Some images work better than others, don't you think?

GraphicsGrid@Partition[(Graphics[Raster[grayGreta /. Thread[garboColors -> #]]] & 
/@ Take[Permutations[{r, gr, b, o, y, w}, {6}], 72]), 12]

garbos galore


Smalltalk (Smalltalk/X), 289 139*

input: i; output: r

r:=i magnifiedTo:75@75.
r colorMapProcessing:[:c||b|b:=c brightness.Color rgbValue:(#(16r0051BA 16rC41E3A 16r009e60 16rff5800 16rFFD500 16rFFFFFF)at:(b*6)ceiling)]

input:

enter image description here

output:

enter image description here

enlarged:

enter image description here

(for all youngsters: this is NOT Madonna ;-)

[*] I have not counted the magnification to 75x75 (the first line) - could have used an already resized as input. Hope that is ok with you.