Cropping all four sides differently

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Misko78
Posts: 10
Joined: 2016-02-13T15:05:36-07:00
Authentication code: 1151

Cropping all four sides differently

Post by Misko78 » 2018-02-08T16:49:02-07:00

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.

User avatar
fmw42
Posts: 22653
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cropping all four sides differently

Post by fmw42 » 2018-02-08T17:15:34-07:00

Create a command with mogrify or convert using -chop 4 times. See https://www.imagemagick.org/Usage/crop/#chop

snibgo
Posts: 9720
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cropping all four sides differently

Post by snibgo » 2018-02-08T18:44:21-07:00

... 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
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22653
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cropping all four sides differently

Post by fmw42 » 2018-02-08T18:52:14-07:00

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

snibgo
Posts: 9720
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cropping all four sides differently

Post by snibgo » 2018-02-08T19:16:29-07:00

Thanks, Fred, I didn't notice:
Misko78 wrote:I use 6.8.1
snibgo's IM pages: im.snibgo.com

User avatar
GeeMack
Posts: 528
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Cropping all four sides differently

Post by GeeMack » 2018-02-08T19:33:52-07:00

Misko78 wrote:
2018-02-08T16:49:02-07:00
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.
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.

User avatar
fmw42
Posts: 22653
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cropping all four sides differently

Post by fmw42 » 2018-02-08T20:06:05-07:00

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

Post Reply