#!/bin/bash ## This file is released under the CC0 license ## https://creativecommons.org/publicdomain/zero/1.0/ # Check if the source image is provided if [ -z "$1" ] || [ ! -f "$1" ]; then if [ -z "$1" ]; then echo "Usage: $0 source_image" else echo "$1 cannot be accessed." fi exit 1 fi SOURCE_IMAGE=$1 BASENAME="$SOURCE_IMAGE" # Function to calculate ssim calculate_ssim() { local source=$1 local target=$2 local ssim ssim=$(magick compare -metric ssim "$source" "$target" null: 2>&1) ssim=${ssim%% *} # Extract the first part before the space echo "$ssim" } # Function to generate images with matched ssim generate_image() { local format=$1 local ssim local tmpfile tmpfile="$(mktemp /tmp/img-XXXX.${format})" for ((q = 10; q <= 100; q += 5)); do convert "$SOURCE_IMAGE" -quality "$q" "$tmpfile" img_ssim=$(calculate_ssim "$SOURCE_IMAGE" "$tmpfile") echo "Format=${format} Q=$q SSIM=${img_ssim}" mv "$tmpfile" "ssim-${img_ssim}_${BASENAME}_q${q}.${format}" done } generate_image "jpg" generate_image "webp" generate_image "avif"