How to split an image vertically using the command line?
Solution 1
Solved it using ImageMagick's convert -crop geometry +repage:
convert -crop 100%x20% +repage image.png image.png
Solution 2
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
- Tile Cropping, sub-dividing one image into multiple images
- ImageMagick v6 Examples -- Cutting and Bordering
Solution 3
Using the "tiles" functionality:
convert image.png -crop 1x5@ out-%d.png
https://www.imagemagick.org/Usage/crop/#crop_tile
Solution 4
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.
Related videos on Youtube
shley
Updated on September 18, 2022Comments
-
shley over 1 year
Say I have a large 800x5000 image; how would I split that into 5 separate images with dimensions 800x1000 using the command line?
-
slm over 9 yearsPlease don't add the solution to your Q. Mark the answer below as accepted.
-
-
slm over 9 yearsThe OP said that this solved it using
convert -crop geometry +repage
. For example:convert -crop 100%x20% +repage image.png image.png
. -
Admin over 9 years
+repage
considerations re: image offset capable formats etc. -
JPT about 5 yearsIf 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
-
CMCDragonkai about 5 yearsHow does this compare to @shley's answer?
-
Evan Nudd about 5 years@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
-
deadfish about 4 yearsOkay, so
100%x20%
splits vertically and20%x100%
splits horizontally. -
cherryblossom over 3 yearsNewer docs: 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.')