Create just a bash script to convert an image to AVIF with Image Magick, it should find…
- User
Create just a bash script to convert an image to AVIF with Image Magick, it should find the right quality value to get a similarity of 99%
- Assistant
Below is an example Bash script that uses ImageMagick’s
convertandcomparetools to “tune” the AVIF compression quality until the SSIM similarity compared to the input image is at least 99%. (Note that you must have ImageMagick compiled with AVIF (often via libheif) support, and thecomparecommand must support the SSIM metric.) This script uses a simple binary search over quality values from 0 to 100 to find the lowest acceptable quality that gives at least 99% similarity.Save the script (for example as
to_avif.sh), make it executable (e.g.chmod +x to_avif.sh), and run it with the input and output file names as arguments.
#!/bin/bash set -e # Check arguments if [ "$#" -ne 2 ]; then echo "Usage: $0 input_image output_image.avif" exit 1 fi input="$1" output="$2" # Temporary file for intermediate AVIF conversion tmp_avif=$(mktemp --suffix=.avif) # Desired SSIM similarity (e.g., 0.99 means 99% similarity) desired_similarity=0.99 # Set initial binary search range for quality (0-100) low=0 high=100 best=100 echo "Tuning quality for SSIM >= $desired_similarity..." while [ "$low" -le "$high" ]; do mid=$(( (low + high) / 2 )) echo " Trying quality: $mid" # Convert the input to AVIF with quality=mid convert "$input" -quality "$mid" "$tmp_avif" # Compare the input and the temporary AVIF using the SSIM metric. # Note: compare prints the metric value to stderr. similarity=$(compare -metric SSIM "$input" "$tmp_avif" null: 2>&1) echo " SSIM similarity: $similarity" # Check if the similarity meets or exceeds the threshold. # The bc tool is used for floating point comparison. if [ "$(echo "$similarity >= $desired_similarity" | bc -l)" -eq 1 ]; then best=$mid # Try to see if a lower quality (smaller file size) still meets the similarity high=$(( mid - 1 )) else low=$(( mid + 1 )) fi done echo "Selected quality value: $best" echo "Converting $input to AVIF with quality=$best ..." # Do the final conversion with the found best quality. convert "$input" -quality "$best" "$output" # Clean up the temporary file rm "$tmp_avif" echo "Done! Output image saved as: $output"
How It Works
-
Input checks and temporary file creation:
The script expects exactly two arguments (the input image and the desired output file name) and creates a temporary file for the intermediate AVIF conversion. -
Binary Search:
It performs a binary search over the integer quality values from 0 to 100. For each quality value (mid):- The script converts the original image to AVIF.
- It then uses the
comparecommand with the SSIM metric (which prints a number between 0 and 1) comparing the original input and the current AVIF image. - If the SSIM is at least 0.99 (i.e. 99% similarity), the script remembers this quality and then continues searching for a possibly lower acceptable quality.
- If the similarity is too low, it increases the quality value.
-
Final conversion:
Once a best quality value is determined, the script runs the conversion a final time to produce the output file. -
Cleanup:
The temporary AVIF file is removed.
Make sure you have the required versions of ImageMagick and
bcinstalled before using the script. Enjoy your AVIF conversions! -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.