The problem is that in version 9 Image
became an atomic object, so it's not possible to extract the image data using Part
any more. Here's a mention of this change.
You need to change every occurrence of someImage[[1,1]]
to ImageData[someImage]
This takes the small images from the example data. It filters only those that have a square aspect ratio.
images = Select[ExampleData /@ ExampleData["TestImage"], Equal @@ ImageDimensions[#] &];
Now let us resize all of them to 50 pixels wide (so they're the same size) and set their magnification to 1. The Magnification
option only affects how they're displayed on screen, but doesn't change the image data. This is useful to have to prevent automatic sizing within Grid
later, and potential alignment problems when the grid doesn't fit in the window.
images = Image[ImageResize[#, 50], Magnification -> 1] & /@ images
These images won't give a very pretty output unfortunately, but I wanted to post self-contained code that doesn't rely on external image files people might not have.
I took the master image from the example data too. Note that here master
holds an image, not a file name, so the rest of the code needs to be changed accordingly. I resized master
to 10 by 10 so the resulting grid will also consist of 10 by 10 small images.
master = ImageResize[ExampleData[{"TestImage", "Lena"}], 10]
The rest is mostly identical, except for the change I mention and for using master
as an image instead of a file name.
imagePool =
Function[i, {i, Mean@Flatten[N@ImageData[i], 1]}] /@ images;
closeMatch[c_] :=
First@RandomChoice@Take[SortBy[imagePool, Norm[ c - #[[2]] ] &], 20];
Grid[Reverse@Map[closeMatch, ImageData[master], {2}],
Spacings -> {0, 0}]
image[[1,1]]
, because I thought the first element of the image object is the pixel map, so the[[1,1]]
doesn't seem to make sense. Correct? $\endgroup$ – Geert Goeteyn Feb 2 '14 at 19:18