Compares images
Inspired by the image compare feature "Visual verification API" of TestApi this code supports comparing images by using a tolerance mask image. That tolerance mask image is a valid image by itself and can be manipulated.
ImageSharpCompare focus on os agnostic support and therefore depends on SixLabors.ImageSharp.
If you run into the licensing hassle around the ImageSharp dependency, consider Codeuctivity/SkiaSharp.Compare as an alternative. It is semi API compatible, and satisfies my personal needs, it may also be the better fit for you. For further details, see Licence Enforcement Changes. The respect here is directed toward the ImageSharp dependency and the work behind it. Keeping this project compliant with license changes takes ongoing effort, and funding would help make it easier to upgrade to the latest ImageSharp versions and keep pace with their technical changes responsibly.
NOTE: The Alpha-channel is ignored.
bool isEqual = ImageSharpCompare.ImagesAreEqual("actual.png", "expected.png");var calcDiff = ImageSharpCompare.CalcDiff("2x2PixelBlack.png", "2x2PixelWhite.png");
Console.WriteLine($"PixelErrorCount: {diff.PixelErrorCount}");
Console.WriteLine($"PixelErrorPercentage: {diff.PixelErrorPercentage}");
Console.WriteLine($"AbsoluteError: {diff.AbsoluteError}");
Console.WriteLine($"MeanError: {diff.MeanError}");
// PixelErrorCount: 4
// PixelErrorPercentage: 100
// AbsoluteError: 3060
// MeanError: 765Imagine two images you want to compare, and want to accept the found difference as at state of allowed difference.
Using CalcDiffMaskImage you can calc a diff mask from actual and reference image
Example - Create difference image
using (var fileStreamDifferenceMask = File.Create("differenceMask.png"))
using (var maskImage = ImageSharpCompare.CalcDiffMaskImage(pathPic1, pathPic2))
SixLabors.ImageSharp.ImageExtensions.SaveAsPng(maskImage, fileStreamDifferenceMask);Example - Compare two images using the created difference image. Add white pixels to differenceMask.png where you want to allow difference.
var maskedDiff = ImageSharpCompare.CalcDiff(pathPic1, pathPic2, "differenceMask.png");
Assert.That(maskedDiff.AbsoluteError, Is.EqualTo(0));

