dotfiles/.local/bin/scripts/shrinkimg

20 lines
767 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;;
2021-12-16 22:04:50 +00:00
(-o) out="$2"; shift 2;;
(-q) quality="$2"; shift 2;;
2022-02-10 14:59:41 +00:00
(x*) resolution="$1"; resize="-resize $resolution"; shift;;
2021-12-16 22:04:50 +00:00
(*) break;;
esac
done
2022-02-10 14:59:41 +00:00
out="${out:-$1${resolution:--shrinked}.jpg}"
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"
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)