Page 1 of 1

-shave and aspect ratio

Posted: 2018-06-08T15:01:37-07:00
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.

Re: -shave and aspect ratio

Posted: 2018-06-08T15:26:25-07:00
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.

Re: -shave and aspect ratio

Posted: 2018-06-08T22:56:16-07:00
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 "%%".

Re: -shave and aspect ratio

Posted: 2018-06-10T08:31:25-07:00
by muccigrosso
"preserve-ratio" seems like a useful option to have on the shave command (or someplace similar).

Re: -shave and aspect ratio

Posted: 2018-06-10T12:37:45-07:00
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

Re: -shave and aspect ratio

Posted: 2018-06-23T04:56:21-07:00
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.