How to Use Montage Picture Produce

1. Import the picture address that you want to make it to montage picture;
2. Import the picture library address that those pictures will combine together to form montage picture;
3. Give the save address to save montage picture;
4. Give the pixel number that original picture will be divided by the pixel number;
5. Give the picture in library pixel size that they will be resize to given pixel size;
6. Run the programming one by one.

Important Things to Mention

1. This Programming will use a huge parts of your computer ram memory, so I strongly suggest you use the low pixel picture production programming.
2. For pixel number, if the pixel size of picture is 1000*1000, I suggest you pixel number should be larger than 5;
3. For picture in library, I suggest you pixel number should be smaller than 100;
4. Picture in library should have same formate (such as jpg, png).

Low Pixel Picture Production

Photo information

picaddress="C:\\Users\\Pictures\\IMG.jpg";
library="C:\\Users\\Pictures\\library";
saveaddress="C:\\Users\\Pictures";
pixelnum=5;
libraryphotopixelnum=100;

Import photo library

Dynamic[picno]
namelist=Import[library];
smallpiclist=Table[0,{i,1,Length[namelist]}];
smallpicblelist=Table[0,{i,1,Length[namelist]}];For[picno=1,picno<Length[namelist]+1,picno++,pic=Import[library<>"\\"<>ToString[namelist[[picno]]]];picdata=ImageData[pic];
smallpic=ImageResize[ImageCrop[pic,{picdata//Dimensions//Drop[#,-1]&//Min,picdata//Dimensions//Drop[#,-1]&//Min}],libraryphotopixelnum];
smallpcidata=ImageData[smallpic];
smallrgb=Table[Sum[smallpcidata[[m,n,i]],{m,1,Dimensions[smallpcidata][[1]]},{n,1,Dimensions[smallpcidata][[2]]}]/(Dimensions[smallpcidata][[1]]*Dimensions[smallpcidata][[2]]),{i,1,3}];

smallpicblend=Blend[{smallpic,RGBColor[smallrgb]}];smallpiclist[[picno]]={smallpic,smallrgb};
smallpicblelist[[picno]]={smallpicblend,smallrgb};]

Import and analyze photo

picture=Import[picaddress];pixnum=pixelnum;parts=Table[ImageTake[picture,{i,i+pixnum},{j,j+pixnum}],{i,0,Dimensions[ImageData[picture]][[1]]-pixnum,pixnum},{j,0,Dimensions[ImageData[picture]][[2]]-pixnum,pixnum}];
picture={};
Monitor[colortable=Table[Table[Sum[ImageData[parts[[p,q]]][[m,n,i]],{m,1,Dimensions[ImageData[parts[[p,q]]]][[1]]},{n,1,Dimensions[ImageData[parts[[p,q]]]][[2]]}]/(Dimensions[ImageData[parts[[p,q]]]][[1]]*Dimensions[ImageData[parts[[p,q]]]][[2]]),{i,1,3}],{p,1,Dimensions[parts][[1]]},{q,1,Dimensions[parts][[2]]}];,{p,q}]
Dimensions[colortable]

Make montage photo

Monitor[com=Table[SortBy[Table[{smallpicblelist[[i,1]],((((smallpiclist[[i,2]]-colortable[[p,q]]))^2)//Total)/3},{i,1,Length[namelist]}],Last][[RandomInteger[{1,2}],1]],{p,1,Dimensions[colortable][[1]]},{q,1,Dimensions[colortable][[2]]}];,{p,q}]

Export photo

Export[saveaddress<>"\\"<>"montage.jpg",ImageAssemble[com]]

High Pixel Picture Production

Photo information

picaddress="C:\\Users\\Pictures\\IMG.png";
library="C:\\Users\\Pictures\\library";
saveaddress="C:\\Users\\Pictures";
pixelnum=4;
libraryphotopixelnum=100;

Import photo library

Dynamic[picno]
namelist=Import[library];
smallpiclist=Table[0,{i,1,Length[namelist]}];
smallpicblelist=Table[0,{i,1,Length[namelist]}];For[picno=1,picno<Length[namelist]+1,picno++,pic=Import[library<>"\\"<>ToString[namelist[[picno]]]];picdata=ImageData[pic];
smallpic=ImageResize[ImageCrop[pic,{picdata//Dimensions//Drop[#,-1]&//Min,picdata//Dimensions//Drop[#,-1]&//Min}],libraryphotopixelnum];
smallpcidata=ImageData[smallpic];
smallrgb=Table[Sum[smallpcidata[[m,n,i]],{m,1,Dimensions[smallpcidata][[1]]},{n,1,Dimensions[smallpcidata][[2]]}]/(Dimensions[smallpcidata][[1]]*Dimensions[smallpcidata][[2]]),{i,1,3}];

smallpicblend=Blend[{smallpic,RGBColor[smallrgb]}];smallpiclist[[picno]]={smallpic,smallrgb};
smallpicblelist[[picno]]={smallpicblend,smallrgb};]

Import and analyze photo

picture=Import[picaddress];pixnum=pixelnum;parts=Table[ImageTake[picture,{i,i+pixnum},{j,j+pixnum}],{i,0,Dimensions[ImageData[picture]][[1]]-pixnum,pixnum},{j,0,Dimensions[ImageData[picture]][[2]]-pixnum,pixnum}];
picture={};
Monitor[colortable=Table[Table[Sum[ImageData[parts[[p,q]]][[m,n,i]],{m,1,Dimensions[ImageData[parts[[p,q]]]][[1]]},{n,1,Dimensions[ImageData[parts[[p,q]]]][[2]]}]/(Dimensions[ImageData[parts[[p,q]]]][[1]]*Dimensions[ImageData[parts[[p,q]]]][[2]]),{i,1,3}],{p,1,Dimensions[parts][[1]]},{q,1,Dimensions[parts][[2]]}];,{p,q}]
Dimensions[colortable]

Make montage photo and Export final photo

CreateDirectory["C:\\Users\\Pictures\\piece"];
Dynamic[row]
For[row=1,row<Dimensions[colortable][[1]]+1,row++,com=Table[SortBy[Table[{smallpicblelist[[i,1]],((((smallpiclist[[i,2]]-colortable[[row,q]]))^2)//Total)/3},{i,1,Length[namelist]}],Last][[RandomInteger[{1,2}],1]],{q,1,Dimensions[colortable][[2]]}];Export["C:\\Users\\Pictures\\piece\\"<>ToString[row]<>".jpg",ImageAssemble[com]];]

pieces=Table[{Import["C:\\Users\\Pictures\\piece\\"<>ToString[j]<>".jpg"]},{j,1,Dimensions[colortable][[1]]}];//AbsoluteTiming

Export[saveaddress<>"\\montage.jpg",ImageAssemble[pieces]]//AbsoluteTiming