Skip to content

Instantly share code, notes, and snippets.

@ideadapt
Last active December 14, 2015 20:49
Show Gist options
  • Save ideadapt/f1aff835963b9d1b3445 to your computer and use it in GitHub Desktop.
Save ideadapt/f1aff835963b9d1b3445 to your computer and use it in GitHub Desktop.
Visual diff between two name matching sets of images. Supports image magick openmp and parallel execution.
# requirements:
# - imagemagick (optionally with openmp)
# - I used this script to diff screen shots taken with selenium.
# Todo so I recommend you to use https://github.com/swissmanu/protractor-screenshot-reporter and overwrite pathBuilder option.
# e.g. `pathBuilder: () => {return path.join(capabilities.caps_.browserName, descriptions.reverse().join('-').replace(/\s/g, "-").replace(/['":,\/]/g, ""));}`
# usage:
# run tests with setup 1
# rm -rf test/run1 && cp -r test/reports/e2e/screenshots/current/chrome/ test/run1
# run tests with setup 2
# rm -rf test/run2 && cp -r test/reports/e2e/screenshots/current/chrome/ test/run2
# NR_CPUS=4 sh ./diffit.sh
# NR_CPUS should not be greater than the number of cores you have (use `convert -bench 2 magick:logo /dev/null` and count number of lines to determine)
# if you have installed image magick with openmp enabled, it gets even faster!
# open test/diffs
#
# only diff images of tests that revealed different screenshots are left over in test/diffs folder.
rm -rf test/diffs
mkdir -p test/diffs
rm -rf test/run1/out && mkdir test/run1/out
rm -rf test/run2/out && mkdir test/run2/out
screens=./test/run1/*.png
count=0
function work() {
screen1="$1"
screen2="${screen/run1/run2}"
scrn1="${screen1/run1/run1/out}"
scrn2="${screen2/run2/run2/out}"
if [ ! -e "$screen2" ]
then
echo "Missing $screen2. Each file in run1 folder must have a matching file in run2 folder."
continue
fi
#echo "converting $screen1 AND $screen2 to $scrn1 AND $scrn2"
# compress
convert -resize 800x800 -quality 90% "$screen1" "$scrn1"
convert -resize 800x800 -quality 90% "$screen2" "$scrn2"
name=`basename "$scrn1"`
difffile="test/diffs/$name"
#echo "generating diff image $name"
compare "$scrn1" "$scrn2" -fuzz 10% -compose src "$difffile"
# remove images with only one color in it (no diff found)
colors=`identify -verbose "$difffile" | grep Colors: | awk '{print $NF}'`
#echo "found $colors colors"
if [ "x$colors" == "x1" ]
then
echo "no diff for $name"
rm "test/diffs/$name"
else
echo "diff found for $difffile"
composite "$difffile" "$scrn1" "$difffile"
fi
}
for screen in $screens
do
work "$screen" &
if [ -n "$NR_CPUS" ]
then
let count+=1
[[ $((count%$NR_CPUS)) -eq 0 ]] && wait
else
wait
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment