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

Fred Weinhaus fmw at alink.net
Sun Feb 17 17:18:13 PST 2008


Dave,

OK, the same script below works for the the other aspect ratio, 
namely 130x180. I have tested it on images of sizes 600x390 (wider 
than tall - aspect=1.53846), 256x256 (square - aspect=1) and 400x520 
(tall than wide - aspect=.76923 and aspect=.69230) for the cases of 
daspect=180/130=1.38461 and daspect=130/180=.72222.  Note that I 
added/moved +repage just after $infile and just before $outfile to be 
sure that the virtual canvas was reset/removed from the infile and 
before creating the outfile; otherwise, things do not work out if the 
virtual canvas is not exactly the size of the input image.


#!/bin/bash

infile="your_infile"
outfile="your_outfile"

dwidth=130	# desired thumbnail width
dheight=180	#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 +repage -resize $resize \
	-gravity Center -crop ${dwidth}x${dheight}+0+0 +repage $outfile



Note the use of the ^ in cases 1 and 3

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


Fred


More information about the Magick-users mailing list