# How to create an ImagePartition type-of-function that generates parts of different sizes

Background: I partition images to polygons ( squares ) and color them with VertexColors. I want to use a minimal number of polygons while still showing a clear resemblance of the original image. - Some images need more squares in the centre than along the edges ( i.e. portraits ). I want to create an ImagePartition type-of-function that uses a finer grid closer to the middle of a picture.

For example:

 1-1 1-2 1-3 1-4 1-5
2-1 .           2-5
3-1 .           3-5
4-1 .           4-5
5-1 5-2 5-3 5-4 5-5
( 5-by-5 )


The missing part

 2-2 2-3 2-4
3-1 3-2 3-3
4-1 4-2 4-3


is replaced with pieces from a 10-by-10 partitioning, i.e. 2-2 by:

 3-3 3-4
4-3 4-4


and so on, until 4-3 is replaced with

  7-7 7-8
8-7 8-8


from the 10-by-10 partitioning.

Finally 3-2 or ( 5-5, 5-6 / 6-5, 6-6 ) is replaced with the results from a 20-by-20 partitioning.

 9-9  9-10  9-11   9-12
10-9 10-10 10-11 10-12
11-9 11-10 11-11 11-12
12-9 12-10 12-11 12-12.


In the case of 6-by-6 partitioning also three levels of partioning are used, in this case 6-by-6, 12-by-12, 24-by-24. And so on.

Question: How to create an alternative ImagePartition function that creates a finer grid closer to the centre of the image. The output lists should be arranged in such a way that it is easy to re-assemble the image from the pieces.

You can iterate ImagePartiton on different sub-levels of previously applied ImagePartiton.

i = ExampleData[{"TestImage", "Lena"}]
p1 = ImagePartition[i, {100, 100}]; p1 // GraphicsGrid


Now pick the middle of your list and replace its elements with finer partiton:

p2 = p1; p2[[2 ;; -2, 2 ;; -2]] = Map[GraphicsGrid[ImagePartition[#, {20, 20}],
Spacings -> 0, Frame -> All, FrameStyle -> White, ImageSize -> 100] &,
p1[[2 ;; -2, 2 ;; -2]], {2}]; Grid[p2, Spacings -> .3]


You can remove all grid wrappers of course - I kept them for visual comprehension. This is easy to pack into a function. You can continue partitioning in the same manner. Is this what you had in mind?

• Wow! - Yes, image-wise this is about what I had in mind, but I need the parts for further processing in my program like ImagePartition. I'll study your answer first. – nilo de roock Aug 1 '12 at 10:52
• Could you elaborate a bit on the Spacings Option you used. I am trying to get an equal spacing overall but that doesn't really work. Also setting spacings to 0 doesn't remove the horizontal spacing but it does so with the vertical spacing. – nilo de roock Aug 1 '12 at 11:41
• @ndroock1 Spacings option is for grid only. I thought you will omit Grid, because you need the list structure only. Nested grid structures are not really good things to have. – Vitaliy Kaurov Aug 1 '12 at 15:29
• Yes, I will. I got it now, although somewhat struggling to make the code generic. But I'll get there. I always do. – nilo de roock Aug 1 '12 at 18:10