45

Say I have a large 800x5000 image; how would I split that into 5 separate images with dimensions 800x1000 using the command line?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
shley
  • 1,081
  • 1
  • 8
  • 7

4 Answers4

52

Solved it using ImageMagick's convert -crop geometry +repage:

convert -crop 100%x20% +repage image.png image.png
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
shley
  • 1,081
  • 1
  • 8
  • 7
31

Using ImageMagick:

$ convert -crop 800x1000 image.png cropped_%d.png

Will create a sequence of files named cropped_1.png, cropped_2.png, and so on.

References

slm
  • 363,520
  • 117
  • 767
  • 871
outlyer
  • 1,093
  • 7
  • 11
  • The OP said that this solved it using `convert -crop geometry +repage`. For example: `convert -crop 100%x20% +repage image.png image.png`. – slm Nov 23 '14 at 14:06
  • 1
    `+repage` [considerations](http://www.imagemagick.org/script/command-line-options.php#crop) re: image offset capable formats etc. –  Nov 23 '14 at 14:18
  • How does this compare to @shley's answer? – CMCDragonkai Mar 21 '19 at 06:51
  • 1
    @CMCDragonkai it's essentially the same, they're using percentages so it will split any size image into 5 vertical slices instead of being written specifically for the 800x5000 case – outlyer Apr 22 '19 at 21:15
9

Using the "tiles" functionality:

convert image.png -crop 1x5@ out-%d.png

https://www.imagemagick.org/Usage/crop/#crop_tile

Luchostein
  • 566
  • 4
  • 7
  • 1
    Newer docs: http://www.imagemagick.org/script/command-line-options.php#crop ('You can add the @ to the geometry argument to equally divide the image into the number of tiles generated.') – cherryblossom Dec 30 '20 at 11:40
1

ImageMagick would crash on me, for the image being too big for it to handle, so I had to resort to other methods.

I ended up using the Python Image Library.

A quick and dirty answer to the OP question follows:

from PIL import Image

im = Image.open("YourImage.yourformat")

for h in range(0, im.height, 1000):
     nim = im.crop((0, h, im.width-1, min(im.height, h+1000)-1))
     nim.save("PartialImage." + str(h) + ".yourformat")

The above code has the final sizes hardcoded, but it can be easily transformed into an full blow script of its own with all inputs parameterized. If one ever needs such a thing.

Fabio A.
  • 141
  • 4