Specifying *minimum* values for -resize option?

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
jamesarobertson

Specifying *minimum* values for -resize option?

Post by jamesarobertson »

Is it possible to specify minimum (rather than maximum) height and width values for the -resize (or other similar) option?

I have images that are a variety of sizes and aspect ratios. I want to take a (square) crop of the middle part of each image, but discard as little as possible of each. To do this I assume I have to first resize each image so that it's smallest dimension is that of the cropping square. Then crop an equal amount from either 'overlapping' side (ie. either top and bottom, if the picture is portrait, or left and right, if the image is landscape).

BTW, I would also like to know if there is a way to crop the centre part of the image without first having to know it's dimensions and calculate where to place the corner point of the cropping rectangle.

If anyone has a simple one-step command for doing this I would be even more grateful :-)

Cheers
JamesR
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Post by glennrp »

Read about the -geometry option. There are tricks with ">", "<", and
"!" to force various behaviors. You can use the geometry string with -resize. You can probably find examples galore on Anthony's web site.

-gravity center can be used with the -crop option to crop out a piece from the center.

Glenn
jamesarobertson

Post by jamesarobertson »

Thanks Glen

The advice about -gravity center was very helpful.

I assume the website you refer to is http://www.cit.gu.edu.au/~anthony/graphics/imagick6/

I have had a good look at this, and the online manual, but still can't figure out how to proportionally scale an image so that it just encompasses a particular sized rectangle (ie. minimum size) - without having to first do a calculation based on the image size.

Cheers
JamesR
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

See IM Examples, resize and thumbnail sections, both have examples of 'minimal resize' to let you crop the middle portion of the image.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jamesarobertson

Post by jamesarobertson »

Thanks Anthony.

After reading http://www.cit.gu.edu.au/~anthony/graph ... space_fill it looks like there is no good way to solve my problem in a single command; since the images I want to produce are quite large (190x190 pixels) and the input images are not predominately portrait nor landscape, so the multiple resizing issue will be a problem in the (tricky) solution suggested here.

Never mind, I have written a script to check the input image dimensions relative to the required dimensions, then resize and crop accordingly.

I really appreciate everyone's help :-)

Cheers
JamesR
Post Reply