ImageMagick - how to enforce min/max heights/widths?
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
UPDA开发者_如何学运维TE:
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.
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.
Using ImageMagick you need only two mogrify
passes: one to enlarge the ones smaller than your required minimum and one to shrink the ones that are larger than the maximum. For instance, to batch resize a bunch of JPEGs:
mogrify '*.jpg[!200x150<]'
mogrify '*.jpg[!275x225>]'
The first pass enlarges the ones smaller than 200x150 and the second one shrinks the ones larger than 275x225 (yes, the signs are correct!). mogrify
replaces the original images, which is what you need to do in this case, to avoid resizing the images all over again in each pass and ending up with two processed copies.
Even though they are two passes you process every image only once (almost, see below). The first processes x% of the images and the other pass processes the remaining 100-x% (plus c, see below), assuming all images needed to be resized.
The ! is used to force change of aspect for images that are outside the range (our constant c), such as an image that is 1200x100, which would not fit in either 275x225 nor 200x150. These images will be processed twice: in the first pass the aspect below the range (height or width) will be augmented to the minimum, and on the second pass the other aspect will be reduced to the maximum.
Hope that helps.
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.
Based on above:
#!/bin/bash
FILE="$1"
DIMS=`identify "${FILE}" | awk '{print $3}'`
WIDTH=`echo "${DIMS}"| cut -dx -f1`
HEIGHT=`echo "${DIMS}"| cut -dx -f2`
max_w=2000
max_h=2000
if [[ $HEIGHT -gt $max_h ]]; then
WIDTH=$(( $WIDTH * $max_h / $HEIGHT ))
HEIGHT="$max_h"
fi
if [[ $WIDTH -gt $max_w ]]; then
HEIGHT=$(( $HEIGHT * $max_w / $WIDTH ))
WIDTH="$max_w"
fi
echo "$FILE ${WIDTH}x${HEIGHT}"
mogrify -filter Lanczos -resize "${WIDTH}x${HEIGHT}" "$FILE"
You can use the tool mogrify that is a part of the imagemagick suite.
There is an example of how to use it here
精彩评论