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

641 Views Asked by At

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!

2

There are 2 best solutions below

3
fmw42 On BEST ANSWER

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.

1
GeeMack On

Here is another approach to splitting an image to create two output images that are 55% from the left and 55% from the right of the original...

convert input.png -set filename:0 "%[t]" \
   \( +clone -roll +55+0% \) -extent 55x100% "%[filename:0]_%d.png"

That starts by setting a file name variable to use for the output. Then it makes a clone of the input image, and rolls it 55% to the right. The result is moving the rightmost 55% of the image out of the frame to the right and back into the frame on the left. Then after the parentheses a simple -extent operation keeps the leftmost 55% of each image. The output files are named from the input file with a "_0" or "_1" added.

As fmw42 suggested, you can make a loop command to run on multiple inputs, and include the path names in the command for your output files.