#!/bin/bash # compressJPG.sh: compress a JPG with mozjpeg if the savings are worthwhile. # Author: Gwern Branwen # Date: 2020-02-07 # When: Time-stamp: "2025-09-30 17:36:48 gwern" # License: CC-0 set -e if [ $# -eq 0 ]; then echo "Error: No arguments given!" exit -1; else command -v jpegtran > /dev/null command -v mogrify > /dev/null JPG="$(mktemp /tmp/XXXXXX-original.jpg)"; TMP="$(mktemp /tmp/XXXXXX-temp.jpg)" cp "$1" "$JPG" jpegtran -copy none -progressive -optimize -outfile "$TMP" "$JPG" mogrify -quality 60 "$TMP" ORIGINAL=$(wc --chars < "$JPG") SMALL=$(wc --chars < "$TMP") RATIO=$(echo "$ORIGINAL / $SMALL" | bc --mathlib) echo "$1 : from $ORIGINAL to $SMALL ($RATIO)" if (( $(echo "$RATIO > 1.2" | bc --mathlib) )); then mv "$TMP" "$1" fi rm "$JPG" "$TMP" &> /dev/null || true shift; if [ $# -eq 0 ]; then exit 0; else compressJPG "$@"; fi; fi