imagemagick

To slice tall image at 100% width and fixed height


I have an image 500 by 20 000 pixels. I need to slice it to smaller ones that are 1000 pixels height and 100% width (I don't want to hard-code width).

For this, I use this one-line script:

input=input.png; width=$(magick identify -format %w $input); magick $input -crop ${width}x1000 +repage output%d.png

But maybe there is a shorter solution? I tried to combine 100% with 1000 pixels, but this doesn't seem to work:

magick input.png -crop 100%x1000 +repage output%d.png

Solution

  • There are several ways to use ImageMagick to crop an image into pieces of a given height and maintain the width of the input image. A very simple method might be...

    magick input.png -crop 0x1000 +repage output%d.png
    

    Using "0" for the crop width will maintain the input width.

    Another similar method uses the variable %[w] for "width"...

    magick input.png -crop %[w]x1000 +repage output%d.png
    

    Addendum by Mark Setchell

    You can omit the zero for the width altogether and it will equally retain the width. You can also use %03d to zero-pad the numbers in the extracted image filenames out to 3 digits, then they will sort in order:

    magick input.png -crop x1000 +repage parts-%03d.png
    

    Results in:

    -rw-r--r--  1 mark  wheel       369 23 Aug 10:13 parts-000.png
    -rw-r--r--  1 mark  wheel       369 23 Aug 10:13 parts-001.png
    -rw-r--r--  1 mark  wheel       369 23 Aug 10:13 parts-002.png
    ...
    ...
    -rw-r--r--  1 mark  wheel       368 23 Aug 10:13 parts-197.png
    -rw-r--r--  1 mark  wheel       367 23 Aug 10:13 parts-198.png
    -rw-r--r--  1 mark  wheel       376 23 Aug 10:13 parts-199.png