I have a large set of jpg images for which I want to create thumbnails. The images all have different sizes and resolutions, but I would like all thumbnails to have a standard size, e.g. 120x80px. However, I do not want to stretch the images. So I would like to do something of the following:
Is there a linux command to do so? I looked into imagemick convert, but I can't figure out how to do the centered cropping. It seems that you have to manually specify the cropping area for each image?
This works for images larger than 120x80. Not tested on smaller ones, but you should be able to tune it.
#! /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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With