Skip to content

Instantly share code, notes, and snippets.

@gingerbeardman
Last active April 16, 2025 23:25
Show Gist options
  • Save gingerbeardman/31c2eabf4c39ebad0ceb9c6265afd5a6 to your computer and use it in GitHub Desktop.
Save gingerbeardman/31c2eabf4c39ebad0ceb9c6265afd5a6 to your computer and use it in GitHub Desktop.
Compares all images in a directory, doesn't check the same pair twice, duplicates are opened in Preview
#!/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"
@gingerbeardman
Copy link
Author

by Matt Sephton, March 2025, MIT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment