这适用于大于120×80的图像.没有在较小的上测试,但你应该能够调整它.
#! /bin/bash
for img in p*.jpg ; do
identify=$(identify "$img")
[[ $identify =~ ([0-9]+)x([0-9]+) ]] || \
{ echo Cannot get size >&2 ; continue ; }
width=${BASH_REMATCH[1]}
height=${BASH_REMATCH[2]}
let good_width=height+height/2
if (( width < good_width )) ; then # crop horizontally
let new_height=width*2/3
new_width=$width
let top='(height-new_height)/2'
left=0
elif (( width != good_width )) ; then # crop vertically
let new_width=height*3/2
new_height=$height
let left='(width-new_width)/2'
top=0
fi
convert "$img" -crop "$new_width"x$new_height+$left+$top -resize 120x80 thumb-"$img"
done