Search code examples
wolfram-mathematicabackwards-compatibilitymathematica-8

Photo Mosaic in Mathematica: an example from 2008 doesn't work in Mathematica 8


I'm trying to get a Mathematica example working. It's the one on Theo Gray's blog.

I think that Mathematica must have changed since he wrote that code (May 2008), since I'm unable to get anything reasonable out of it, despite changing nearly everything. Do I use ImageData instead of Import? Can anyone suggest a version of this code that works for Mathematica 8?

imagePool = 
 Map[With[{i = Import[#]}, {i, Mean[Flatten[N[i[[1, 1]]], 1]]}] &, 
  FileNames["Pool/*.jpg"]];
closeMatch[c_] := 
  RandomChoice[Take[SortBy[imagePool, Norm[c - #[[2]]] &], 20]][[1]];
Grid[Reverse[
  Map[closeMatch, Import["MendeleevIcon.tif"][[1, 1]], {2}]], 
  Spacings -> {0, 0}]

Solution

  • The following works (Thanks to @yoda for pointing out the Reverse[] thing in the comments):

    f = FileNames["*.jpg", {"c:\\test\\pool\\Pool"}];
    m = Import["c:\\test\\pool\\Pool\\MendeleevIcon.tif"];
    imagePool =
      Map[
       With[{i = Import[#]},
         {i, Mean[Flatten[ImageData@i, 1]]}] &, f];
    closeMatch[c_] := 
      RandomChoice[Take[SortBy[imagePool, Norm[c - #[[2]]] &], 20]][[1]];
    Grid[Map[closeMatch, ImageData@m, {2}], Spacings -> {0, 0}]
    

    enter image description here