Page 1 of 1

Cropping all four sides differently

Posted: 2018-02-08T16:49:02-07:00
by Misko78
I use 6.8.1 q16

Is it possible to crop 15px from the right side, 5 px from top, 25 from bottom and 10 from the left side? I use a SP3000 scanner and it leaves mask on all four side, i would like to do a batch job. Thanks.

Re: Cropping all four sides differently

Posted: 2018-02-08T17:15:34-07:00
by fmw42
Create a command with mogrify or convert using -chop 4 times. See https://www.imagemagick.org/Usage/crop/#chop

Re: Cropping all four sides differently

Posted: 2018-02-08T18:44:21-07:00
by snibgo
... Or do it in a single "-crop", with fx expressions:

Code: Select all

magick in.tiff -crop %[fx:w-25]x%[fx:h-30]+10+5 +repage out.tiff

Re: Cropping all four sides differently

Posted: 2018-02-08T18:52:14-07:00
by fmw42
Snibgo's command will require IM 7. It won't work on IM 6. But you can create the width and height arguments in IM 6 in separate steps putting them in variables and then use the variables in the crop command.

Unix syntax:

Code: Select all

width=`convert -ping input.suffix -format "%[fx:w-25]" info:`
height=`convert -ping input.suffix -format "%[fx:h-30]" info:`
convert input.suffix -crop ${width}x${height}+10+5 +repage out.suffix

Re: Cropping all four sides differently

Posted: 2018-02-08T19:16:29-07:00
by snibgo
Thanks, Fred, I didn't notice:
Misko78 wrote:I use 6.8.1

Re: Cropping all four sides differently

Posted: 2018-02-08T19:33:52-07:00
by GeeMack
Misko78 wrote: 2018-02-08T16:49:02-07:00Is it possible to crop 15px from the right side, 5 px from top, 25 from bottom and 10 from the left side? I use a SP3000 scanner and it leaves mask on all four side, i would like to do a batch job.
You can make a Windows BAT script that uses one ImageMagick command to do the calculations and reset the canvas dimensions to create that result.

Code: Select all

convert input.png -set page "%%[fx:w-10-15]x%%[fx:h-5-25]-10-5" -flatten output.png
Note the double percent signs "%%" at the beginning of the FX expressions. Those are required to use that command in a BAT script. Make those into single percent signs "%" to use this command as a *nix script or from the Windows command line.

Re: Cropping all four sides differently

Posted: 2018-02-08T20:06:05-07:00
by fmw42
Nice approach for IM 6, GeeMack!

Another IM 6 approach would be to use distort SRT with viewport cropping.

See https://www.imagemagick.org/Usage/disto ... t_viewport