Search code examples
imagemagickmogrify

ImageMagick: Split image into two with 55% of original image each


I want to split some images by percentage using ImageMagick. To be more precise, I want to split an image into two images. The output-image of the left side should be 55% of the left side of the original image, the output-image of the right side should be 55% of the right ride of the original image.

(In case I am not making myself clear: The point is that there is sometimes important information in the middle of the images and this information would be cut off if we split the images exactly in the middle.)

What I have so far is this: mogrify -crop 55%x100% +repage -path ./cropped *.jpg

So at the moment, I am splitting the image into two and saving it in the "cropped"-folder. BUT, only the left side is 55% of the left side of the original image, the right side is 45% of the right side of the original image.

As this is my first time working with ImageMagick, I would really appreciate your help!


Solution

  • I think you would have to run a script loop over each image and use convert rather than mogrify. Mogrify can only output one image for each input image. You do not say what platform/OS or what version of ImageMagick. The following assumes Unix-like system with IM 6 and is one way to do that.

    infile="original_file.suffix"
    convert "$infile" -set filename:fname "%t" -write mpr:img +delete \
    \( mpr:img -gravity west -crop 55x100%+0+0 +repage +write "path_to/cropped/%[filename]_0.suffix" \) \
    \( mpr:img -gravity east -crop 55x100%+0+0 +repage +write "path_to/cropped/%[filename]_1.suffix" \) \
    null:
    

    Or, you could just run mogrify twice. Once for the left side and once for the right side. Use -gravity to control which as in the convert command.