#!/bin/bash if [[ -z "$1" || -z "$2" ]]; then echo "Usage: $0 " exit 1 fi TMP=$(mktemp --suffix=.png) echo "Comparing [$1] to [$2]" echo "Saving difference in $TMP" echo DENSITY=100 # this supports both simple file names and page indexed file names like: # file[0] file[1] - will either return one line for each page, or a single # line if a single page is specified PAGES1=$(magick identify "$1" | wc -l) PAGES2=$(magick identify "$2" | wc -l) if (( PAGES1 != PAGES2 )); then echo "Number of pages between documents does not match: $PAGES1 != $PAGES2" echo "Only comparing the first page." magick compare -density "$DENSITY" -background white "$1[0]" "$2[0]" "$TMP" PHASH_DIFF=$(~/bin/pdf-compare-phash "$1[0]" "$2[0]") elif (( PAGES1 > 5 )); then echo "Too many pages ($PAGES1 > 5) to create hyper-image with all pages." echo "Only comparing first page." magick compare -density "$DENSITY" -background white "$1[0]" "$2[0]" "$TMP" PHASH_DIFF=$(~/bin/pdf-compare-phash "$1[0]" "$2[0]") else # convert the PDFs into a single image with the pages vertically stacked ALL1=$(mktemp --suffix=.png) magick convert -density "$DENSITY" "$1" -append "$ALL1" ALL2=$(mktemp --suffix=.png) magick convert -density "$DENSITY" "$2" -append "$ALL2" magick compare -density "$DENSITY" -background white "$ALL1" "$ALL2" "$TMP" PHASH_DIFF=$(~/bin/pdf-compare-phash "$ALL1" "$ALL2") fi if [ "$TERM_PROGRAM" = "iTerm.app" ]; then echo "Visual difference between images:" echo "--------------------------------" imgcat-small "$TMP" echo "--------------------------------" else open "$TMP" fi echo "Perceptual hash difference (0 is exactly the same): $PHASH_DIFF"