dotfiles/.local/bin/scripts/shrinkimg

33 lines
895 B
Plaintext
Raw Normal View History

2022-02-10 14:59:41 +00:00
#!/bin/sh -e
2021-12-16 22:04:50 +00:00
# Lossy image compression using ImageMagick
2022-02-10 14:59:41 +00:00
# Eliminates artifacts and metadata
while true
2021-12-16 22:04:50 +00:00
do case $1 in
2022-02-10 14:59:41 +00:00
(-h|--help|"") echo "Usage: $0 [-q quality (default 85)] [-o outfile] [xRES] <images...>" && exit 2;;
2024-01-12 16:32:16 +00:00
(-o) out="$2"; shift;;
(-q) quality="$2"; shift;;
(x*) resolution="$1"; resize="-resize $resolution";;
(-v) set -x;;
2021-12-16 22:04:50 +00:00
(*) break;;
esac
2024-01-12 16:32:16 +00:00
shift
done
2024-01-12 16:32:16 +00:00
process() {
out=$1
shift
magick "$@" -auto-orient -strip \
-interlace Plane -define jpeg:dct-method=float -sampling-factor 4:2:0 -gaussian-blur 0.05 \
-quality "${quality:-85}" $resize "$out"
}
if test -n "$out"
then process "$out" "$@"
else
for arg
do process "${arg}${resolution:--shrinked}.jpeg" "$arg"
done
fi
2021-12-16 22:04:50 +00:00
printf "Shrinked $1(%s) to $out(%s) - reduced to %s%%\n" \
2021-12-20 17:34:03 +00:00
$(stat --format %s "$1" "$out" | numfmt --to=iec-i --suffix=B) \
2021-12-16 22:04:50 +00:00
$(stat --format %s "$out" "$1" | sed 'N;s|\n|*100/|' | bc)