Skip to content
TheChatArchiveThe Chat Archive
grok-3-preview-02-24· xAIHugging Face LMArena Human Preference 140K

Create just a bash script to convert an image to AVIF with Image Magick, it should find…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-4.1-mini-2025-04-14’s answer.
  1. 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%

  2. Assistant

    Here's a bash script that uses ImageMagick to convert an image to AVIF format while finding the optimal quality setting to achieve approximately 99% similarity (using SSIM - Structural Similarity Index) compared to the original image:

    #!/bin/bash
    
    # Check if ImageMagick is installed
    if ! command -v magick &> /dev/null; then
        echo "Error: ImageMagick is not installed. Please install it first."
        exit 1
    fi
    
    # Check if input file is provided
    if [ $# -ne 1 ]; then
        echo "Usage: $0 input_image"
        exit 1
    fi
    
    input_file="$1"
    output_file="${input_file%.*}.avif"
    
    # Check if input file exists
    if [ ! -f "$input_file" ]; then
        echo "Error: Input file '$input_file' does not exist."
        exit 1
    fi
    
    # Function to get SSIM value between two images
    get_ssim() {
        local original="$1"
        local converted="$2"
        ssim=$(magick compare -metric SSIM "$original" "$converted" null: 2>&1)
        echo "$ssim"
    }
    
    # Function to convert image and return SSIM
    convert_and_check() {
        local quality="$1"
        local temp_output="temp.avif"
        
        # Convert to AVIF with given quality
        magick "$input_file" -quality "$quality" "$temp_output"
        
        # Get SSIM value
        ssim=$(get_ssim "$input_file" "$temp_output")
        
        # Clean up temp file
        rm "$temp_output"
        
        echo "$ssim"
    }
    
    # Binary search to find optimal quality
    target_ssim=0.99
    min_quality=0
    max_quality=100
    best_quality=0
    best_ssim=0
    tolerance=0.001  # How close we want to get to target SSIM
    
    echo "Finding optimal quality for 99% similarity..."
    
    while [ $((max_quality - min_quality)) -gt 1 ]; do
        mid_quality=$(((min_quality + max_quality) / 2))
        
        # Convert and get SSIM
        current_ssim=$(convert_and_check "$mid_quality")
        
        echo "Testing quality $mid_quality: SSIM = $current_ssim"
        
        # Update best values if this is the best so far
        if (( $(echo "$current_ssim > $best_ssim" | bc -l) )); then
            best_ssim=$current_ssim
            best_quality=$mid_quality
        fi
        
        # If we're close enough to target, break
        if (( $(echo "$current_ssim > $target_ssim - $tolerance && $current_ssim < $target_ssim + $tolerance" | bc -l) )); then
            break
        fi
        
        # Adjust search range
        if (( $(echo "$current_ssim < $target_ssim" | bc -l) )); then
            min_quality=$mid_quality
        else
            max_quality=$mid_quality
        fi
    done
    
    # Perform final conversion with best quality
    echo "Converting with optimal quality: $best_quality (SSIM: $best_ssim)"
    magick "$input_file" -quality "$best_quality" "$output_file"
    
    echo "Conversion complete! Output saved as: $output_file"
    

    This script:

    1. Checks for ImageMagick installation and proper usage
    2. Takes an input image file as an argument
    3. Uses binary search to find the optimal quality setting that achieves approximately 99% similarity
    4. Uses SSIM (Structural Similarity Index) as the quality metric
    5. Performs test conversions at different quality levels
    6. Creates the final AVIF file with the optimal quality setting

    To use the script:

    1. Save it as convert_to_avif.sh
    2. Make it executable: chmod +x convert_to_avif.sh
    3. Run it: ./convert_to_avif.sh input_image.jpg

    Requirements:

    • ImageMagick with AVIF support
    • bc (basic calculator) for floating-point comparisons

    Notes:

    • The script uses SSIM as a quality metric, which is generally better than PSNR for perceptual quality
    • The target similarity is set to 99% (SSIM = 0.99)
    • The script uses binary search to efficiently find the right quality setting
    • The tolerance is set to 0.001, meaning it will accept SSIM values between 0.989 and 0.991
    • The output file will have the same name as the input but with .avif extension

    The script will output progress information showing the quality levels it's testing and their corresponding SSIM values, and finally create the AVIF file with the optimal quality setting.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.