By adopting the answers from Mr. Wizard to my previous question, I tried to create a stacked-alphabet image from a person's portrait, the alphabets were taken from the name of the portrait subject. Here is an example of Marilyn Monroe.
Marilyn Monroe's portrait:
Result:
The problem is: Why some of the alphabets,"i", "l" and their combined alphabets turned into color images. And how to obtain a faster version of the code below.
(* change alphabets to images *)
alpha2Im[allst_] :=
ImageResize[
ImageCrop@
Graphics[
Text[Style[#, 24, FontFamily -> "Helvetica",
Antialiasing -> True]] &~MapThread~{allst } ] , {18, 18}];
(* choose appropriate alphabet-image from the alphabet image list *)
chooseAlp[imV_, len_] := (For[i = 1, i <= len, i++,
If[alphImageGrayPair[[i]][[2]] >= imV,
Return[alphImageGrayPair[[i]][[1]]] ];
])
Clear[i, l, r];
name = "Marilyn Monroe";
im = Import["https://i.sstatic.net/MnwCU.jpg"] ;
alphabets =
Subsets[Union@Characters@StringReplace[name, " " -> ""], 2];
alphImageLst = alpha2Im[#] & /@ alphabets ;
alphGrayValLst = Mean@Flatten@ImageData[#] & /@ alphImageLst;
alphImageGrayPair =
Sort[MapThread[
List, {alphImageLst, alphGrayValLst}], #1[[2]] < #2[[2]] &];
imD = ImageData@im;
{imW, imH} = ImageDimensions@im ;
avg = ConstantArray[0, {imW/4, imH/4}];
ddata = Partition[imD, {4, 4}, 4];
For[i = 1, i <= imH/4, i++,
For[j = 1, j <= imW/4, j++,
avg[[i, j]] = Mean@Flatten[ddata[[i, j]], 1];]]
allStrips = {};
strip = {};
For[j = 1, j <= imH/4, j++,
strip = {};
For[k = 1, k <= imW/4, k++,
AppendTo[ strip, chooseAlp[ avg[[j, k]], imW/4 ] ];
];
AppendTo[allStrips, strip ];
];
allStrips // ImageAssemble