Possible Duplicate:
How to combine images with the same dimensions in a grid?

I would like to arrange images without any space between their edges as each piece is made to connect with others.

The images should not necessarily be merged but simply tiled.

My ultimate purpose is to use a Manipulate to generate different arrangements.

So, how can I place those images in a coordinate system so I can define 9 positions and assign those locations to the images without any space between them ?

Below my attempt using Grid and 2 example of the desired Output once I figure this out. Those composition were generated using Java Scripts.


The problem with Grid or ImageAssemble is they would not allow for "empty slots", which is an option I would like to have as shown in the last image for example.

enter image description here

enter image description here

enter image description here

  • $\begingroup$ Do the results you want differ in any way from this question? $\endgroup$ – rcollyer Jan 26 '12 at 14:13
  • $\begingroup$ @rcollyer, I am having trouble to express it, but what I need is to be able to assign positions from 1 to 9 as I will implement some rules about the "neighborhood". I would group the elements by their attributes : Color or B&White for example and say for example that no B&W should be close to a color one. So i am looking for the best system that will enable me that. I am happy to merge or delete if you think they are redundant. $\endgroup$ – 500 Jan 26 '12 at 14:46
  • $\begingroup$ So what you need is code to organize the images, not necessarily merge them together, correct? $\endgroup$ – rcollyer Jan 26 '12 at 14:47
  • $\begingroup$ @rcollyer, Yes. The underlying subject is I tried to create sets of elements that can be re arranged in many way. I want to use mathematica to generate and select a lot of combination. While now they are all tiled together I might want to have empty locations at some point. $\endgroup$ – 500 Jan 26 '12 at 14:52
  • $\begingroup$ @rcollyer, maybe this will help laeh500.com/LAEH/Meet_Arto.html $\endgroup$ – 500 Jan 26 '12 at 14:53

One can in fact use the (once documented) third argument of Rectangle[] to tile images. Here's an example I cooked up:

imgs = {ExampleData[{"TestImage", "Clock"}], 
   ExampleData[{"TestImage", "Elaine"}], 
   ExampleData[{"TestImage", "JellyBeans"}], 
   ExampleData[{"TestImage", "Lena"}], 
   ExampleData[{"TestImage", "Mandrill"}], 
   ExampleData[{"TestImage", "Peppers"}], 
   ExampleData[{"TestImage", "Splash"}], 
   ExampleData[{"TestImage", "Tiffany"}], 
   ExampleData[{"TestImage", "U2"}]};

mat = Partition[RandomSample[Range[9]], 3];
Graphics[Table[Rectangle[{j, k}, {j + 1, k + 1}, imgs[[mat[[j + 1, k + 1]]]]],
               {j, 0, 2}, {k, 0, 2}]]

random images

The idea is that, you have nine images to distribute on a 3×3 grid; you thus partition some permutation of the numbers $1$ to $9$ in groups of three (that is, mat), have the indices of Table[] map to a corresponding entry of mat, and then that entry of mat has a corresponding index in the list imgs. The extension to any number of images to be arranged in a grid should be straightforward.

  • $\begingroup$ Does this allow for empty slots in the array? $\endgroup$ – rcollyer Jan 26 '12 at 15:32
  • 2
    $\begingroup$ Just use a conditional as needed: If[j == 2 && k == 1, (* then *) {White, Rectangle[{j, k}, {j + 1, k + 1}]}, (* else *) Rectangle[{j, k}, {j + 1, k + 1}, imgs[[mat[[j + 1, k + 1]]]]]] $\endgroup$ – J. M. is in limbo Jan 26 '12 at 15:39
  • $\begingroup$ @J.M., Thank you very much ! $\endgroup$ – 500 Jan 26 '12 at 15:45

If your images are the same size, a quick and dirty hack is using ImageAssemble in place of Grid. (I used that before several times.)

You can generate "empty slots" for use with ImageAssemble like this: Rasterize[Graphics[{}, ImageSize -> ImageDimensions[existingImage]], "Image"]. Adjust the required image size as needed.

Otherwise the Grid option to use should be Spacings, but I always have some trouble with that:

Mathematica graphics

Regarding importing images, you can consider building on the following simpler code:

ImageResize[#, 256] & /@ Import /@ FileNames["B*.png"]
  • $\begingroup$ beat me to it by 2 seconds $\endgroup$ – Heike Jan 26 '12 at 14:11
  • $\begingroup$ @Heike There's still the question of doing this properly (no gaps) with Grid though ... $\endgroup$ – Szabolcs Jan 26 '12 at 14:17
  • $\begingroup$ Setting Spacings->{0,0} works on my system. $\endgroup$ – Heike Jan 26 '12 at 14:19
  • $\begingroup$ @Szabolcs, Thank You ! Could you explain me the use of "*" in the importing function ? $\endgroup$ – 500 Jan 26 '12 at 14:47
  • $\begingroup$ @500 FileNames["B*.png"] will give you all files start with a B and end in .png in the current directory. The keyword to google for is wildcard. $\endgroup$ – Szabolcs Jan 26 '12 at 14:49

For what it's worth, when working at magnification 200% I get no gaps in Grid using Spacing -> {0, 0}. You could therefore Rasterize and ImageResize from there as needed.

Mathematica graphics

  • $\begingroup$ Wizard, thank you ! $\endgroup$ – 500 Jan 26 '12 at 15:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.