#!/bin/sh ratio_w=533 ratio_h=300 sentinel=margins choose_extent() { local ratio_w="$1" ratio_h="$2" input="$3" identify -format '%w %h\n' "$input" | { read w h || return w_prime=$((h * ratio_w / ratio_h)) if [ "$w_prime" -gt "$w" ] then w=$w_prime else h=$((w * ratio_h / ratio_w)) fi echo ${w}x${h} } } for input do case "$input" in *.*) output=${input%.*}.$sentinel.${input##*.} ;; *) output=$input.$sentinel.png ;; esac [ -f "$input" ] || continue extent=$(choose_extent $ratio_w $ratio_h "$input") || continue convert "$input" -gravity center -extent "$extent" "$output" done