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.
-shave and aspect ratio
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: -shave and aspect ratio
You need to do inline computations of the aspect ratio and the amount to shave. Try these (Unix syntax for the variables)
Input:
Alternately, you can compute the crop width and height and use -gravity center -crop.
Input:
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
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: -shave and aspect ratio
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...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).
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
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
-
- Posts: 64
- Joined: 2017-10-03T10:39:52-07:00
- Authentication code: 1151
Re: -shave and aspect ratio
"preserve-ratio" seems like a useful option to have on the shave command (or someplace similar).
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: -shave and aspect ratio
Here is another approach using the new -crop ratio.
This command seems simple enough that I do not believe any new define is needed
Code: Select all
xshave=50
yshave=0
magick input -set option:ratio "%w:%h" -shave "${xshave}x${yshave} -gravity center -crop "%[ratio]" +repage output
Re: -shave and aspect ratio
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.
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.