Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 6 down vote accepted

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

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

enter image description here

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]

enter image description here

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?

share|improve this answer
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

It seems quad-tree decomposition would suit your needs: enter image description here

share|improve this answer
That looks great! Would you please include executable code here, and a description of how to use it? – Mr.Wizard Aug 3 '12 at 17:10
@Mr.Wizard - Good answer, I agree, although not useful in my app. But you ask the OP to copy code from the Wolfram Demos site. Isn't the link sufficient? – nilo de roock Aug 7 '12 at 9:31
@ndroock1 generally links alone are not considered good StackExchange answers, but I don't have time to find references for that statement right now. – Mr.Wizard Aug 7 '12 at 9:37
I'll take your word for it @Mr.Wizard – nilo de roock Aug 7 '12 at 10:12
@Mr.Wizard I'll have to leave that to the interested readers. I just do not have the time right now to look into the potential licensing issues relevant to copying code from the Wolfram Demonstration website. – Matthias Odisio Aug 8 '12 at 15:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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