Lego Art is a theme launched by Lego in 2020. This series features sets themed around famous figures and characters from popular culture, enabling enthusiasts to recreate them in a mosaic style using 1x1 Lego tiles or plates. Each set measures 48x48 pieces. Constructed on nine 16x16 base plates and joined.
I have LEGO® Art – Art Project – Create Together. Following the instructions included in this set, I can make mosaics of baskets, dinosaurs, cakes, and more. I would like to use this set to create something based on the iconic pop culture figures and characters of the other sets.

Create Character

Prepare a character to be made into Lego Art using DALL-E.
prompt:
Create an illustration of Einstein using only vivid colors, strong contrasts, and pop art-like features. Keep Einstein’s signature tongue out, white hair and deep wrinkles, but recreate his image with vivid colors and simplified shapes. Draw a close-up of the face. The torso, suit, tie, and clothing need not be drawn. The background should be dark blue.
In[]:=
img=
;

Make Mosaic

Make a 48x48 dimension mosaic of the character created by DALL-E.
In[]:=
size=48;​​mosaic=ImageResize[img,{size}];​​{y,x}=ImageDimensions@mosaic;​​Rasterize[mosaic,RasterSize->400,ImageSize->150]
Out[]=

Make Lego Art : Part 1

Partition the mosaic into array of 1x1 pixel subimages.
In[]:=
imgparts=ImagePartition[mosaic,1];
Get the dominant colors in each subimages.
In[]:=
dominantcols=ParallelMap[DominantColors[#,1][[1]]&,imgparts,{2}];
When the tiles of each dominant color are laid down, Lego Art is created.
In[]:=
bgcol=
;​​tiles1=Map[Rasterize[Graphics[{#,Disk[]}],Backgroundbgcol,ImageSize->24]&,dominantcols,{2}];​​legoart1=ImageAssemble@tiles1;
In[]:=
Rasterize[legoart1,RasterSize->400,ImageSize->150]
Out[]=
However, the tile color variety used here is 1805 and my set contains only 16 tile colors, and the number of tiles of each color is limited in each.
In[]:=
Flatten/*Union/*Length@dominantcols
Out[]=
1805

Make Lego Art : Part 2

We need to convert the mosaic to Lego Art obeying the tile set restrictions of a set.
The colors and number of tiles in my set are as follows
In[]:=
pcs={{"Black",
,254},{"Dark Blue",
,660},{"Dark Azure",
,127},{"Light Aqua",
,317},{"White",
,474},{"Bright Pink",
,58},{"Red",
,190},{"Orange",
,121},{"Bright Light Orange",
,175},{"Yellow",
,299},{"Bright Light Yellow",
,119},{"Tan",
,241},{"Medium Nougat",
,281},{"Reddish Brown",
,307},{"Bright Green",
,148},{"Lime",
,166}};
The number of tile colors is 16.
In[]:=
colcnt=Length@pcs​​pcscols=pcs[[All,2]]
Out[]=
16
Out[]=
{
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
}
The number of tiles of each color is as follows
In[]:=
pcscnt=pcs[[All,3]]
Out[]=
{254,660,127,317,474,58,190,121,175,299,119,241,281,307,148,166}
The total number of tiles is 3937.
In[]:=
pcscnt//Total
Out[]=
3937
Here in advance, make images of the tiles for the three types that will be used later.
In[]:=
createtiles[pcs_]:=Block{pcscols,colcnt,pcscnt,numberingtile,tiles,tilesNoBlack,tilesNoWhite},​​(*colors*)​​pcscols=pcs[[All,2]];​​(*numberofcolors*)​​colcnt=Length@pcs;​​(*numberoftilesforeachcolor*)​​pcscnt=pcs[[All,3]];​​​​(*tileswithnumber*)​​numberingtile[col_,no_,bg_]:=Block{nocol,edgecol},​​nocol=
[◼]
BlackWhiteContrast
[col];​​edgecol=If[col==Black,White,Black];​​Rasterize[Graphics[{EdgeForm[{Thickness[.03],edgecol}],col,Disk[],Inset[Style[ToString@no,14,nocol]]},Backgroundbg],ImageSize->24]​​;​​​​tiles=Rasterize[Graphics[{#,Disk[]},Backgroundbgcol],ImageSize->24]&/@pcscols;​​tilesNoBlack=MapIndexed[numberingtile[#1,First@#2,Black]&,pcscols];​​tilesNoWhite=MapIndexed[numberingtile[#1,First@#2,White]&,pcscols];​​​​{tiles,tilesNoBlack,tilesNoWhite}​​;
In[]:=
{tiles,tilesNoBlack,tilesNoWhite}=createtiles[pcs];​​Grid@%
Out[]=
Tiles must be laid so that the total distance between the dominant color and tile at each location is minimized, subject to the following constraints
con1 : One tile may be used per location.
con2 : There is a limit to the number of tiles of each color used.
Define variables.
In[]:=
digits=3;​​v[i_,j_,k_]:=Symbol["p"<>StringPadLeft[ToString[i],digits,"0"]<>StringPadLeft[ToString[j],digits,"0"]<>StringPadLeft[ToString[k],digits,"0"]];​​vars=Flatten@Table[v[i,j,k]∈NonNegativeIntegers,{i,colcnt},{j,y},{k,x}];
Define the function of the total distance between the dominant color and tile at each location
In[]:=
distances=ParallelMap[Nearest[pcscols->All,#,Length@pcscols]&,dominantcols,{2}];f=Flatten/*Total@​​Table[v[i,j,k]*Sort[distances[[j,k]],#1[[2]]<#2[[2]]&][[i,3]],{j,y},{k,x},{i,colcnt}];
Define the con1 : One tile may be used per location.
In[]:=
con1=And@@Flatten@Table
colcnt
∑
i=1
v[i,j,k]==1,{j,y},{k,x};
Define the con2 : There is a limit to the number of tiles of each color used.
In[]:=
con2=And@@(Table[Flatten/*Total@Table[v[i,j,k],{j,y},{k,x}]<=pcscnt[[i]],{i,colcnt}]);
Find values of variables vars that minimize f.
In[]:=
res=LinearOptimization[f,{con1,con2},vars];
Based on the solution, lay tiles that are similar to the dominant color of the mosaic, and we have Lego Art.
In[]:=
ans=ArrayReshape[res,{colcnt,y,x}];​​matrix=Table[Pick[Range[colcnt],Values@ans[[All,j,k]],1][[1]],{j,y},{k,x}];​​​​assemble[pcs_,matrix_]:=Block[{tiles,tilesNoBlack,tilesNoWhite},​​{tiles,tilesNoBlack,tilesNoWhite}=createtiles@pcs;​​ImageAssemble[tiles[[#]]&/@matrix]​​];legoart=assemble[pcs,matrix];
Show four images side by side.

Usage of Tiles

Show how much of each color tile my Lego Art uses. All red tiles are used, and any missing tiles are substituted with tiles of another color.

Make Building Instructions

The actual Lego Art set includes instructions on how to make it, so let’s make instructions in a similar format.
Make nine instructions for 16x16 base plates.
Number the 16 colored tiles from 1 to 16 and illustrate how they are placed on each 16 x16 base plate.

Integrate

Integrating the above,
If you have a Lego Art, please have fun with this.

CITE THIS NOTEBOOK

Making Lego Art from Images
by Kotaro Okazaki​
Wolfram Community, STAFF PICKS, December 13, 2023
​https://community.wolfram.com/groups/-/m/t/3082695