[magick-users] RE: Thumbnails with center auto crop

Fred Weinhaus fmw at alink.net
Sun Feb 17 16:47:39 PST 2008


Dave,


The following bash script seems to work. There are 3 cases you need 
to consider. If the aspect ratio is greater than 180/130, if the 
aspect ratio is less than 180/130 but greater than or equal to 1, if 
the aspect ratio is less than 1.

#!/bin/bash

infile="your_infile"
outfile="your_outfile"

dwidth=180	# desired thumbnail width
dheight=130	#desired thumbnail height


width=`identify -format "%w" $infile`	# actual image width
height=`identify -format "%h" $infile`	# actual image height
aspect=`echo "scale=5; $width / $height" | bc`	# actual image aspect ratio
daspect=`echo "scale=5; $dwidth / $dheight" | bc`	# desired 
thumbnail aspect ratio

test1=`echo "$aspect > $daspect" | bc`	# returns 1 if true and 0 if false
test2=`echo "$aspect >= 1" | bc`	# returns 1 if true and 0 if false

if [ $test1 -eq 1 ]
	then
	echo "aspect larger than desired aspect (wider than tall)"
	resize="${dheight}x${dheight}^"
elif [ $test2 -eq 1 ]
	then
	echo "aspect less than desired aspect (wider than tall) but 
greater than 1"
	resize="${dwidth}x${dwidth}"
else
	echo "aspect less than 1 (taller than wide)"
	resize="${dwidth}x${dwidth}^"
fi

convert $infile -resize $resize +repage \
	-gravity Center -crop ${dwidth}x${dheight}+0+0 +repage $outfile



Note the use of the ^ in cases 1 and 3


Note: I have not checked this yet for the case where your desired 
width is less than your desired height (e.g. 130x180 for example). 
This may require an alternate version.

I will try to make a proper script of this for any desired aspect 
ratio over the next few days and add it to my script page.


Fred



>Hehe, that was a little confusing, sorry. So you're saying do 
>something like this:
>
>convert 1.jpg -filter Lanczos -resize 180x130^ -gravity center 
>1_.jpg but it wont work for square images.
>
>Dave


More information about the Magick-users mailing list