-shave and aspect ratio

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
EFEIMCMNT
Posts: 2
Joined: 2018-06-08T14:36:13-07:00
Authentication code: 1152

-shave and aspect ratio

Post by EFEIMCMNT »

Hello ImageMagic Community,
I'm here with a silly question, fore sure answered already somewhere, sorry for asking again, I cannot find a working solution.

I'm using the package ImageMagick-7.0.7-38-Q16-x64-dll.exe on a Win10Pro-x64.
I cannot manage to have -shave option to preserve original image aspect ratio.
I want to crop a border around the image of a certain number of pixels, I want to specify how may pixel to remove for width (or height) and I want ImageMagik to calculate how many pixel to remove for height (or width).

I'm using the following command line:
convert orig.jpg -shave 50 shaved.jpg
With an orig.jpg of 8920x3222, I always obtain a shaved.jpg = 8820x3122 which is a crop of -100x-100 which is not preserving the aspect ratio.
I would like to obtain either 8820x3186 (crop of -100x-36) or 8643x3122 (crop of -277x-100)
Whatever I've tried I always get the same crop for width and height which alter the aspect ratio.

I would appreciate your patient help.

Kinds Regards,
EFE.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -shave and aspect ratio

Post by fmw42 »

You need to do inline computations of the aspect ratio and the amount to shave. Try these (Unix syntax for the variables)

Input:
Image

Code: Select all

width=50
magick barn.jpg -format "%[fx:w/h]\n" info:
1.33779
magick barn.jpg -shave "${width}x%[fx:round($width*h/w)]" barn_aspect_shave.jpg
magick barn_aspect_shave.jpg -format "%[fx:w/h]\n" info:
1.33333

Code: Select all

height=50
magick barn.jpg -format "%[fx:w/h]\n" info:
1.33779
magick barn.jpg -shave "%[fx:round($height*w/h)]x${height}" barn_aspect_shave.jpg
magick barn_aspect_shave.jpg -format "%[fx:w/h]\n" info:
1.33668
Alternately, you can compute the crop width and height and use -gravity center -crop.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: -shave and aspect ratio

Post by GeeMack »

EFEIMCMNT wrote: 2018-06-08T15:01:37-07:00I cannot manage to have -shave option to preserve original image aspect ratio.
I want to crop a border around the image of a certain number of pixels, I want to specify how may pixel to remove for width (or height) and I want ImageMagik to calculate how many pixel to remove for height (or width).
As fmw42 mentioned, IM can calculate the necessary amount to remove using "-crop" or "-extent". Here are a couple examples. To shave 50 pixels from the left and right side, and shave the top and bottom as needed to maintain the original aspect, a command like this should work...

Code: Select all

set SHAVE=50
magick input.png -gravity center -extent "%[fx:w-(%SHAVE%*2)]x%[fx:h-((%SHAVE%*2)*h/w)]" result.png
If you want to specify shaving 50 pixels from the top and bottom, and calculate the amount to shave from the left and right, this command should work...

Code: Select all

set SHAVE=50
magick input.png -gravity center -extent "%[fx:w-((%SHAVE%*2)*w/h)]x%[fx:h-(%SHAVE%*2)]" result.png
To use these in a BAT script you need to change the single percent signs "%" on the FX expressions to doubles "%%".
muccigrosso
Posts: 64
Joined: 2017-10-03T10:39:52-07:00
Authentication code: 1151

Re: -shave and aspect ratio

Post by muccigrosso »

"preserve-ratio" seems like a useful option to have on the shave command (or someplace similar).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -shave and aspect ratio

Post by fmw42 »

Here is another approach using the new -crop ratio.

Code: Select all

xshave=50
yshave=0
magick input -set option:ratio "%w:%h" -shave "${xshave}x${yshave} -gravity center -crop "%[ratio]" +repage output
This command seems simple enough that I do not believe any new define is needed
EFEIMCMNT
Posts: 2
Joined: 2018-06-08T14:36:13-07:00
Authentication code: 1152

Re: -shave and aspect ratio

Post by EFEIMCMNT »

Folks,
many thanks all ! I really appreciated your help on this.
You actually saved me a lot of time as I was going in the wrong direction.

Again many thanks for your patience in explaining things in very clear form.

Regards.
Post Reply