Skip to content

Instantly share code, notes, and snippets.

@onebytegone
Created November 2, 2023 12:11
Show Gist options
  • Save onebytegone/fd72aea5e3c3529ca2fd25885b4c1db1 to your computer and use it in GitHub Desktop.
Save onebytegone/fd72aea5e3c3529ca2fd25885b4c1db1 to your computer and use it in GitHub Desktop.
Imagemagick Image Compare Script
#!/usr/bin/env bash
# Usage: ./image-compare.sh "${URL_A}" "${URL_B}"
# Example: ./image-compare.sh https://placekitten.com/g/200/300 https://placekitten.com/200/300
TMP_DIR=$(mktemp -d)
IMG_A_URL="$1"
IMG_A_FILENAME=$(echo "${IMG_A_URL}" | sed -E 's|^.*/([^/]+)(\?.*)?$|\1|')
IMG_A_EXT=$(echo "${IMG_A_FILENAME}" | sed -E 's|^.*\.(.+)|\1|')
IMG_B_URL="$2"
IMG_B_FILENAME=$(echo "${IMG_B_URL}" | sed -E 's|^.*/([^/]+)(\?.*)?$|\1|')
IMG_B_EXT=$(echo "${IMG_B_FILENAME}" | sed -E 's|^.*\.(.+)|\1|')
curl -s "${IMG_A_URL}" -o "${TMP_DIR}/a.${IMG_A_EXT}"
curl -s "${IMG_B_URL}" -o "${TMP_DIR}/b.${IMG_B_EXT}"
magick compare \
-metric AE -fuzz 5% \
-compose src \
"${TMP_DIR}/a.${IMG_A_EXT}" "${TMP_DIR}/b.${IMG_B_EXT}" "${TMP_DIR}/compare.png"
cat << EOF > "${TMP_DIR}/diff.html"
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>${IMG_A_FILENAME} vs ${IMG_B_FILENAME}</title>
</head>
<body>
<img src="a.${IMG_A_EXT}">
<img src="b.${IMG_B_EXT}">
<img src="compare.png">
</body>
</html>
EOF
open "${TMP_DIR}/diff.html"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment