29

Please consider following example:

enter image description here

The source image consists of 6 areas that need to be sliced up into 6 separate images.

How can I get the desired output using imagemagick. I tried to understand a possible solution presented in the imagemagick examples, but failed to transfer it to my specific problem.

What would be a way of solving this problem preferably in a one-liner? Since all the areas which i want to slice have the same size, but only differ in their offset, is there a way to somehow pass a preset area size, and then simply add the xy-offset for each area?

jottr
  • 3,256
  • 3
  • 29
  • 35
  • How do they differ in offset? Arbitrarily placed? A grid starting at an origin? Basically, how do the offsets of the areas relate to each other? – sorpigal Mar 09 '12 at 15:45

1 Answers1

42

If each area has the same amount of padding around it, you can use the @ operator.

This cuts an image into 6 sections, 2 per row, with 40 pixels of horizontal padding and 20 pixels of vertical padding excluded from each section:

convert image.png -crop 2x3-40-20@ +repage +adjoin tile-%d.jpg
0eggxactly
  • 4,642
  • 1
  • 16
  • 16
  • Can I do this but starting at `%d` =1 ? – theonlygusti Feb 05 '18 at 19:18
  • Yes, see [fx expressions](http://www.imagemagick.org/script/fx.php) and [filename percent escapes](https://www.imagemagick.org/Usage/files/#save_escapes) -- `convert image.png -crop 2x3-40-20@ -set filename:index "%[fx:t+1]" +repage +adjoin tile-%[filename:index].jpg` – 0eggxactly Mar 14 '18 at 21:15