Page 1 of 1

How to "best fit" (crop/resize)

Posted: 2016-10-07T07:06:43-07:00
by squadjot
Hi all!

i'd like to crop images to fit a certain size (filling the format with the image)

To illustrate what i want i created the desired effect in HTML/CSS http://edweb.dk/Temp/crop.html

As you can see the image is centered and filling the container, the rest is cropped away.

How to achieve this with IM?

I'm using ImageMagick-6.8.9-0 cmd on windows

I tried fiddling with a script.. but it doesent work.. this simply just resizes the image

Code: Select all

convert myfile.jpg -resize 280x400 -extent 280x400 -quality 90 converted.jpg
Hope someone can help me, thanks

Re: How to "best fit" (crop/resize)

Posted: 2016-10-07T07:49:38-07:00
by Marsu42
squadjot wrote:Hope someone can help me, thanks
You need to somehow calculate the exact part that you want to crop, I'm doing this outside im with a shell script after reading the image props. Then set the gravity % crop this area, then resize. Lookie here under "{size}{+-}x{+-}y": http://www.imagemagick.org/script/comma ... p#geometry

Re: How to "best fit" (crop/resize)

Posted: 2016-10-07T09:36:45-07:00
by fmw42
If your new size does not have the same aspect ratio as the original, then you will have to either 1) pad, 2) crop or 3) distort

Unix syntax:

PAD:

Code: Select all

convert A_original.jpg -resize 280x400 -gravity center -background black -extent 280x400 -quality 90 converted.jpg
CROP:

Code: Select all

convert A_original.jpg -resize 280x400^ -gravity center -extent 280x400 -quality 90 converted.jpg
DISTORT:

Code: Select all

convert A_original.jpg -resize 280x400! -gravity center -extent 280x400 -quality 90 converted.jpg
In Windows syntax, escape the ^ as ^^

Re: How to "best fit" (crop/resize)

Posted: 2016-10-07T10:44:24-07:00
by squadjot
fmw42 wrote: CROP:

Code: Select all

convert A_original.jpg -resize 280x400^ -gravity center -extent 280x400 -quality 90 converted.jpg
I'll try this, thanks!

Re: How to "best fit" (crop/resize)

Posted: 2016-10-07T11:21:08-07:00
by fmw42
On windows, you will need to escape the ^ as ^^

Code: Select all

convert A_original.jpg -resize 280x400^^ -gravity center -extent 280x400 -quality 90 converted.jpg

Re: How to "best fit" (crop/resize)

Posted: 2016-10-07T15:36:10-07:00
by squadjot
I can not get it to work :/

My bat file contains following:

Code: Select all

convert %1 -resize 280x400^^ -gravity center -extent 280x400 -quality 90 converted.jpg
And this is what i get:
Image

Re: How to "best fit" (crop/resize)

Posted: 2016-10-07T15:50:10-07:00
by snibgo
If you also uploaded your input file, we could test with it and find the problem. But you didn't.

Re: How to "best fit" (crop/resize)

Posted: 2016-10-07T19:35:00-07:00
by fmw42
Using the first image in your upload as the input (A_original.jpg), it works fine for me on IM 6.9.6.0 and 6.8.9.0 Q16 Mac OSX and resizes and crops the image to fill the window. Windows syntax needs the ^ escaped to ^^ as you seem to have done. But your result seems to be as if it was not using the ^^.

Try

Code: Select all

convert %1 -resize "280x400^^" -gravity center -extent 280x400 -quality 90 converted.jpg
Try running it in a command window with the exact same file I used directly in the command without a variable for the input file. Were you running it in a bat script?

Are you using the same input image that I am. If not please provide that.

Re: How to "best fit" (crop/resize)

Posted: 2016-10-09T14:47:33-07:00
by squadjot
Using quotes worked

Code: Select all

convert %1 -resize "280x400^" -gravity center -extent 280x400 -quality 90 converted.jpg
Both a single ^ and double ^^ worked

Thanks alot!, i would never have figured that out myself 8)