Resize then crop

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
jusanthermemory
Posts: 2
Joined: 2011-04-16T19:39:57-07:00
Authentication code: 8675308

Resize then crop

Post by jusanthermemory »

I'm trying to take images that are large and resize them down to 275x275. This isn't working since using -resize is keeping the aspect ratio so what I want to do is resize so that both height and width are a minimum of 275 and then crop the image with dimensions 275x275. I'd like to keep aspect ratio if at all possible but if I can't do it and have it fill up the 275x275 then that's fine as well. Is this possible?
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: Resize then crop

Post by NicolasRobidoux »

Consult Anthony Thyssen's ImageMagick Examples:http://www.imagemagick.org/Usage/

Code that does this and more is here: http://imagemagick.org/discourse-server ... 4&start=15
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resize then crop

Post by fmw42 »

jusanthermemory wrote:I'm trying to take images that are large and resize them down to 275x275. This isn't working since using -resize is keeping the aspect ratio so what I want to do is resize so that both height and width are a minimum of 275 and then crop the image with dimensions 275x275. I'd like to keep aspect ratio if at all possible but if I can't do it and have it fill up the 275x275 then that's fine as well. Is this possible?

convert image -resize "275x275^" -gravity center -crop 275x275+0+0 +repage resultimage

see http://www.imagemagick.org/script/comma ... p#geometry regarding ^ and other character options.

This resizes according to the smaller of your input image dimensions keeping aspect ratio. Thus the smaller dimension will end up as 275, but the larger dimension will be larger. Thus the crop following the resize.

On Windows you may have to escape the ^ with ^ so that it is ^^ (if the quotes don't help). See http://www.imagemagick.org/Usage/windows/



Alternately, you can resize to the larger dimension and then pad the image

convert image -resize "275x275" -gravity center -background white -extent 275x275 resultimage



Alternately, you can resize and lose aspect ratio ( and accept some distortion)

convert image -resize "275x275!" resultimage

examples are at http://www.imagemagick.org/Usage/resize/#fill
jusanthermemory
Posts: 2
Joined: 2011-04-16T19:39:57-07:00
Authentication code: 8675308

Re: Resize then crop

Post by jusanthermemory »

Thanks both of you for your help and guides. I was fumbling around with the guide posted by NicolasRobidoux using the Cut the Thumbnail to Fit method but wasn't using the original image field. Now that I think about it, that makes no sense. I got it working how I want with "convert image -resize "275x275^" -gravity center -crop 275x275+0+0 +repage resultimage". I actually tried this earlier but I used mogrify and it just made all the images super tiny but switching it out with convert did the trick.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resize then crop

Post by fmw42 »

mogrify should work, but I suggest using a different directory to store the results. Say your images are in folder1 and you create a new folder2

cd folder1
mogrify -path /pathto/folder2 -format jpg -resize "275x275^" -gravity center -crop 275x275+0+0 +repage *.jpg

The multiple input images are *.jpg or (* for all types). The resulting format/image will be the same name as the input but with type determined by -format, in this case also jpg, but you can change it if you want.

For -format jpg, the +repage is not needed as jpg does not store the virtual canvas. For other types, it is needed, so it does no harm to put that in so as to reset the virtual canvas to the cropped size.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Resize then crop

Post by anthony »

A summery of all the resize and pad/crop methods are in IM Examples Thumbnailing, starting with "padding".
http://www.imagemagick.org/Usage/thumbnails/#pad
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Pgscannell
Posts: 2
Joined: 2016-08-12T18:43:54-07:00
Authentication code: 1151

Re: Resize then crop

Post by Pgscannell »

I use magick.exe. Will that application work the same way as convert.exe does? So would I simply execute:

magick orig_image,jp -resize "275x275" -gravity center -background white -extent 275x275 new_image.jpg

You only need the double-quotes around the -resize parameter and not the -extent one?

Also, what are the possible values for the -gravity parameter?


Thanks,
Paul
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resize then crop

Post by snibgo »

"magick" works more or less like "convert". See the porting guide for differences: http://www.imagemagick.org/script/porting.php

For gravity options, type "magick -list gravity". Or see the options documentation http://www.imagemagick.org/script/comma ... hp#gravity

In previous posts, the resize had a special character, caret ^, hence the quotes.
snibgo's IM pages: im.snibgo.com
Post Reply