46

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

1
  • 2
    Please don't add the solution to your Q. Mark the answer below as accepted.
    – slm
    Nov 23, 2014 at 14:03

4 Answers 4

55

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

convert -crop 100%x20% +repage image.png image.png
6
  • 4
    If you want to apply this to a batch of files, try this: ls -1 *.png | sed 's,.*,& &,' | xargs -n 2 convert -crop 100%x20% +repage
    – JPT
    Feb 28, 2019 at 20:55
  • 2
    Okay, so 100%x20% splits vertically and 20%x100% splits horizontally.
    – deadfish
    Mar 9, 2020 at 14:23
  • To place the exports in a subfolder, create that folder and add it to the "sed" part of the command: ls -1 *.png | sed 's,.*,& subfolder_name/&,' | xargs -n 2 convert -crop 100%x20% +repage
    – Tin Man
    Oct 12, 2022 at 9:10
  • To split a scan of two pages, I had to use the option -deconstruct. Without it one of the two files came out as the original with one side blank. Feb 7 at 13:32
  • Seems no need to +repage in latest versions?
    – Shaohua Li
    Mar 19 at 5:44
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

4
  • 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, 2014 at 14:06
  • 1
    +repage considerations re: image offset capable formats etc.
    – user44370
    Nov 23, 2014 at 14:18
  • How does this compare to @shley's answer? Mar 21, 2019 at 6: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, 2019 at 21:15
10

Using the "tiles" functionality:

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

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

1
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .