How to "best fit" (crop/resize)

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
squadjot
Posts: 8
Joined: 2016-10-07T06:58:41-07:00
Authentication code: 1151

How to "best fit" (crop/resize)

Post 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
User avatar
Marsu42
Posts: 75
Joined: 2014-06-12T03:17:45-07:00
Authentication code: 6789
Location: Berlin

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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 ^^
squadjot
Posts: 8
Joined: 2016-10-07T06:58:41-07:00
Authentication code: 1151

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

Post 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!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
squadjot
Posts: 8
Joined: 2016-10-07T06:58:41-07:00
Authentication code: 1151

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo »

If you also uploaded your input file, we could test with it and find the problem. But you didn't.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
squadjot
Posts: 8
Joined: 2016-10-07T06:58:41-07:00
Authentication code: 1151

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

Post 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)
Post Reply