imageimagemagickimagemagick-convert

How to crop a image into several rectangular grids using imagemagick


How can I cut a large image into a grid so that the smaller images can be uploaded to Instagram, to make up the large image in the grid view?

I think imagemagick can be used for this.


Solution

  • I have no idea what an Instagram grid is or what size constraints it might have, but if you have an image like this:

    enter image description here

    You can divide it into a grid, 3 tiles wide by 2 high like this:

    magick input.jpg -crop 3x2@ tile-%d.png
    

    And here are the 6 tiles:

    -rw-r--r--@ 1 mark  staff   62199  2 Jun 16:26 tile-0.png
    -rw-r--r--@ 1 mark  staff   75180  2 Jun 16:26 tile-1.png
    -rw-r--r--@ 1 mark  staff   69615  2 Jun 16:26 tile-2.png
    -rw-r--r--@ 1 mark  staff  108443  2 Jun 16:26 tile-3.png
    -rw-r--r--@ 1 mark  staff  121714  2 Jun 16:26 tile-4.png
    -rw-r--r--@ 1 mark  staff  121384  2 Jun 16:26 tile-5.png
    

    enter image description here

    If you are cropping into lots of smaller parts, you are better using a zero-padded tile name like this so that they occur listed in order if you wish to re-assemble them.:

    magick input.jpg -crop 5x4@ tile-%04d.png
    

    enter image description here

    -rw-r--r--  1 mark  staff   5976  2 Jun 16:33 tile-0000.png
    -rw-r--r--  1 mark  staff  15138  2 Jun 16:33 tile-0001.png
    -rw-r--r--  1 mark  staff  17625  2 Jun 16:33 tile-0002.png
    -rw-r--r--  1 mark  staff  15640  2 Jun 16:33 tile-0003.png
    -rw-r--r--  1 mark  staff  12695  2 Jun 16:33 tile-0004.png
    -rw-r--r--  1 mark  staff  30138  2 Jun 16:33 tile-0005.png
    -rw-r--r--  1 mark  staff  32371  2 Jun 16:33 tile-0006.png
    -rw-r--r--  1 mark  staff  30280  2 Jun 16:33 tile-0007.png
    -rw-r--r--  1 mark  staff  33469  2 Jun 16:33 tile-0008.png
    -rw-r--r--  1 mark  staff  29507  2 Jun 16:33 tile-0009.png
    -rw-r--r--  1 mark  staff  34697  2 Jun 16:33 tile-0010.png
    -rw-r--r--  1 mark  staff  36322  2 Jun 16:33 tile-0011.png
    -rw-r--r--  1 mark  staff  36616  2 Jun 16:33 tile-0012.png
    -rw-r--r--  1 mark  staff  40337  2 Jun 16:33 tile-0013.png
    -rw-r--r--  1 mark  staff  37466  2 Jun 16:33 tile-0014.png
    -rw-r--r--  1 mark  staff  30444  2 Jun 16:33 tile-0015.png
    -rw-r--r--  1 mark  staff  36170  2 Jun 16:33 tile-0016.png
    -rw-r--r--  1 mark  staff  39400  2 Jun 16:33 tile-0017.png
    -rw-r--r--  1 mark  staff  38850  2 Jun 16:33 tile-0018.png
    -rw-r--r--  1 mark  staff  36439  2 Jun 16:33 tile-0019.png
    

    It seems a little cruel not to tell you how to reassemble them... so, if you want to reassemble them, you can use:

    magick montage -geometry +0+0 -tile 5x4 tile-????.png rebuilt.png
    

    If, for some unknown reason, you want to reassemble the tiles with a 1px wide and 5px tall yellow line between the tiles:

    magick montage -background yellow -geometry +1+5 -tile 5x4 tile-????.png rebuilt.png
    

    enter image description here