views:

390

answers:

2

Using ImageMagick, how can I resize an image to have a minimum:

  • height of 150px
  • width of 200px

and also have a maximum:

  • height of 225px
  • width of 275px

UPDATE:

In case it helps, here's a further explanation of what I'm experiencing.

I have a buch of images with all different ratio dimensions. Some images have 1:5 height/width ratios. Some have 5:1 height/width ratios. So that I want to do is set that a minimum height/width size for the image but also don't want the image size to be larger than a particular size.

If I need to apply white padding to an image to make it fit within my constraint so that I don't have to distort the image, I'd like to do so.

A: 

I can't entirely get my head around your requirement, but I think this should be possible if you run IM twice.

See the manual on geometry options:

A combination of

widthxheight> Change as per widthxheight but only if an image dimension exceeds a specified dimension.

and

widthxheight^ Minimum values of width and height given, aspect ratio preserved.

might do the trick. However, for images whose aspect ratio lies outside your requirements, I think you will have to create a fixed size canvas in IM, fill it with some colour, insert the image, and do a trim() on it... Probably not possible in one go, though.

Pekka
So what would the command line code look like? *convert widthxheight^=200x150 widthxheight=275x225*
Henryh
@HenryH I don't think it works that way. You would have to make two runs on every image, one time bringing it up to the minimum size (if necessary) one time bringing it down (if necessary).
Pekka
Would you mind updating your answer post to have the code necessary to do this. thanks
Henryh
@HenryH sorry, I don't have Imagemagick handy right now to try out what sequence exactly is necessary (I would have to trial and error, too, I don't work with IM that often). Maybe somebody else can come up with it.
Pekka
@HenryH the basic idea, though, just to be clear, is to call `convert` twice on the same image. Once to bring it to minimum size, one to cut it down to maximum size. That should be pretty straight forward to do.
Pekka
A: 

OK, I do some similar processing. I don't have a generic solution for arbitrary sizes and aspect ratios, but if your input set of images is either 1:5 or 5:1, you can group them into 2 categories and process accordingly using some of these code snippets.

Step 1: determine which aspect ratio the image is, and based on that, set your target final altered dimensions.

INPATH="path/to/your_file.jpg"
DIMS=`identify "${INPATH}" | awk '{print $3}'`
WIDTH=`echo "${DIMS}"| cut -dx -f1`
HEIGHT=`echo "${DIMS}"| cut -dx -f2`
if [[ ${WIDTH} > ${HEIGHT} ]]
then
    #file is in landscape orientation
    THUMBDIM="80x60"
    WEBDIM="624x480"
else
    #file is in portrait orientation
    echo -n "(Portrait orientation) "
    THUMBDIM="60x80"
    WEBDIM="480x624"
fi

Step 2, do the actual resize:

convert "${INPATH}" -resize "${THUMBDIM}" "resized-${INPATH}"

This sample assumes a fixed set of desired sizes, but of course you could do some basic arithmetic using your original dimensions as input and scaling up in proportion to fit within your stated boundaries.

Peter Lyons