summaryrefslogtreecommitdiff
path: root/dot/local/bin/letterbox
blob: b56e795ec1ce5993632e05d7b6d71086099f2a3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/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