Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 473
Siddharth Dushantha Avatar asked Jan 27 '23 06:01

Siddharth Dushantha


2 Answers

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
like image 60
Mark Setchell Avatar answered Mar 29 '23 23:03

Mark Setchell


To make any image into a grid of squares with ImageMagick you need to decide the number of units in advance. A command like this will start by cropping an input image into an exact square, then crop that square into a 3x3 grid of smaller squares...

convert in.png -gravity center -extent 1:1 -crop 3x3@ out%02d.png

That "-extent" crops the input to the largest possible square so when it's cut into a 3x3 grid the finished images are square, too. To crop the image into a 3x4 grid you'll use a command more like this...

convert in.png -gravity center -extent 3:4 -crop 3x4@ out%02d.png

In that example the "-extent" crops the input image to an exact aspect ratio of 3:4 so when you crop it into 3 pieces by 4 pieces they'll all be squares.

Both examples will produce output images with sequentially numbered file names like "out01.png", "out02.png", etc.

If you want to number the output images in the order you need to upload them, you'll probably want that numbering in reverse. You can add "-reverse -scene 1" to the command just before writing the outputs to get the file names of those cropped squares numbered in the order you'll use for uploading.

If you're using IM7, change the "convert" to "magick" in those commands.

Note: The syntax that allows "-extent" to use an aspect ratio like "3:4" has only been available since early 2018. Using older versions of ImageMagick might require manually calculating for that first crop to get the input image to the proper aspect ratio (... or using FX expressions to set a viewport and "-distort" to simulate the crop).

like image 36
GeeMack Avatar answered Mar 30 '23 00:03

GeeMack