#!/usr/bin/env zsh # Threshold for similarity THRESHOLD=0.05 # Get a sorted list of PNG files, using null-terminated strings to handle special characters files=() while IFS= read -r -d '' file; do files+=("$file") done < <(find . -maxdepth 1 -type f -name "*.png" -print0 | sort -z) # Find similar images in a directory for ((i=1; i<=${#files}; i++)); do echo -n "." for ((j=i+1; j<=${#files}; j++)); do file1="${files[i]}" file2="${files[j]}" # Run comparison in background ( # Use printf to safely escape filenames for the compare command similarity=$(compare -metric AE "${file1}" "${file2}" null: 2>&1) # Extract just the numeric value, handling both possible output formats similarity_float=$(echo "$similarity" | sed 's/[()]//g' | awk '{print $1}') # Check if similarity_float is actually a number and compare if [[ "$similarity_float" =~ ^[0-9]*\.?[0-9]+$ ]]; then result=$(echo "$similarity_float < $THRESHOLD" | bc -l 2>/dev/null) if [ "$result" -eq 1 ]; then echo -n "#" # Safely open files open "${file1}" "${file2}" fi fi ) & done done wait echo "\nComparison complete"