blink-diff
Blink-Diff
A lightweight image comparison tool
Table of Contents
- Installation
- Usage
- Examples
- API-Documentation
- Tests
- Project Focus
- Project Naming
- Contributions
- Contributors
- Third-party libraries
- License
##Image Comparison and Result
##Installation
Install this module with the following command:
npm install blink-diff
Add the module to your package.json dependencies:
npm install --save blink-diff
Add the module to your package.json dev-dependencies:
npm install --save-dev blink-diff
##Usage
The package can be used in two different ways:
- per command line
- through an object
###Command-Line usage
The command-line tool can be found in the bin directory. You can run the application with
blink-diff --output <output>.png <image1>.png <image2>.png
Use image1 and image2 as the images you want to compare.
Only PNGs are supported at this point.
The command-line tool exposes a couple of flags and parameters for the comparison:
--verbose Turn on verbose mode
--debug Turn on debug mode - leaving all filters and modifications on the result
--threshold p Number of pixels/percent 'p' below which differences are ignored
--threshold-type t 'pixel' and 'percent' as type of threshold. (default: pixel)
--delta p Max. distance colors in the 4 dimensional color-space without triggering a difference. (default: 20)
--copyImageA Copies first image to output as base. (default: true)
--copyImageB Copies second image to output as base.
--no-copy Doesn't copy anything to output as base.
--output o Write difference to the file 'o'
--filter f Filters f (separated with comma) that will be applied before the comparison.
--no-composition Turns the composition feature off
--compose-ltr Compose output image from left to right
--compose-ttb Compose output image from top to bottom
--hide-shift Hides shift highlighting (default: false)
--h-shift Acceptable horizontal shift of pixel. (default: 0)
--v-shift Acceptable vertical shift of pixel. (default: 0)
--block-out x,y,w,h Block-out area. Can be repeated multiple times.
--version Print version
--help This help
###Object usage
The package can also be used directly in code, without going through the command-line.
Example:
var diff = new BlinkDiff({
imageAPath: 'path/to/first/image', // Use file-path
imageBPath: 'path/to/second/image',
thresholdType: BlinkDiff.THRESHOLD_PERCENT,
threshold: 0.01, // 1% threshold
imageOutputPath: 'path/to/output/image'
});
diff.run(function (error, result) {
if (error) {
throw error;
} else {
console.log(diff.hasPassed(result.code) ? 'Passed' : 'Failed');
console.log('Found ' + result.differences + ' differences.');
}
});
All the parameters that were available in the command-line tool are also available through the class constructor, however they might use slightly different wording. The class exposes additional parameters that are not available from the command-line:
-
imageAPathDefines the path to the first image that should be compared (required; imageAPath or imageA is required – see example below) -
imageASupplies first image that should be compared (required; imageAPath or imageA is required – see example below) – This can be a PNGImage instance or a Buffer instance with PNG data -
imageBPathDefines the path to the second image that should be compared (required; imageBPath or imageB is required – see example below) -
imageBSupplies second image that should be compared (required; imageBPath or imageB is required – see example below) – This can be a PNGImage instance or a Buffer instance with PNG data -
imageOutputPathDefines the path to the output-file. If you leaves this one off, then this feature is turned-off. -
imageOutputLimitDefines when an image output should be created. This can be for different images, similar or different images, or all comparisons. (default: BlinkDiff.OUTPUT_ALL) -
verboseVerbose output (default: false) -
thresholdTypeType of threshold check. This can be BlinkDiff.THRESHOLD_PIXEL and BlinkDiff.THRESHOLD_PERCENT (default: BlinkDiff.THRESHOLD_PIXEL) -
thresholdNumber of pixels/percent p below which differences are ignored (default: 500) – For percentage thresholds: 1 = 100%, 0.2 = 20% -
deltaDistance between the color coordinates in the 4 dimensional color-space that will not trigger a difference. (default: 20) -
outputMaskRedRed intensity for the difference highlighting in the output file (default: 255) -
outputMaskGreenGreen intensity for the difference highlighting in the output file (default: 0) -
outputMaskBlueBlue intensity for the difference highlighting in the output file (default: 0) -
outputMaskAlphaAlpha intensity for the difference highlighting in the output file (default: 255) -
outputMaskOpacityOpacity of the pixel for the difference highlighting in the output file (default: 0.7 – slightly transparent) -
outputShiftRedRed intensity for the shift highlighting in the output file (default: 255) -
outputShiftGreenGreen intensity for the shift highlighting in the output file (default: 165) -
outputShiftBlueBlue intensity for the shift highlighting in the output file (default: 0) -
outputShiftAlphaAlpha intensity for the shift highlighting in the output file (default: 255) -
outputShiftOpacityOpacity of the pixel for the shift highlighting in the output file (default: 0.7 – slightly transparent) -
outputBackgroundRedRed intensity for the background in the output file (default: 0) -
outputBackgroundGreenGreen intensity for the background in the output file (default: 0) -
outputBackgroundBlueBlue intensity for the background in the output file (default: 0) -
outputBackgroundAlphaAlpha intensity for the background in the output file (default: undefined) -
outputBackgroundOpacityOpacity of the pixel for the background in the output file (default: 0.6 – transparent) -
blockOutObject or list of objects with coordinates that should be blocked before testing. -
blockOutRedRed intensity for the block-out in the output file (default: 0) This color will only be visible in the result when debug-mode is turned on. -
blockOutGreenGreen intensity for the block-out in the output file (default: 0) This color will only be visible in the result when debug-mode is turned on. -
blockOutBlueBlue intensity for the block-out in the output file (default: 0) This color will only be visible in the result when debug-mode is turned on. -
blockOutAlphaAlpha intensity for the block-out in the output file (default: 255) -
blockOutOpacityOpacity of the pixel for the block-out in the output file (default: 1.0) -
copyImageAToOutputCopies the first image to the output image before the comparison begins. This will make sure that the output image will highlight the differences on the first image. (default) -
copyImageBToOutputCopies the second image to the output image before the comparison begins. This will make sure that the output image will highlight the differences on the second image. -
filterFilters that will be applied before the comparison. Available filters are: blur, grayScale, lightness, luma, luminosity, sepia -
debugWhen set, then the applied filters will be shown on the output image. (default: false) -
compositionCreates as output a composition of all three images (approved, highlight, and build) (default: true) -
composeLeftToRightCreates comparison-composition from left to right, otherwise it lets decide the app on what is best -
composeTopToBottomCreates comparison-composition from top to bottom, otherwise it lets decide the app on what is best -
hShiftHorizontal shift for possible antialiasing (default: 2) Set to 0 to turn this off. -
vShiftVertical shift for possible antialiasing (default: 2) Set to 0 to turn this off. -
hideShiftUses the background color for “highlighting” shifts. (default: false) -
cropImageACropping for first image (default: no cropping) – Format: { x:, y:, width:, height: } -
cropImageBCropping for second image (default: no cropping) – Format: { x:, y:, width:, height: } -
perceptualTurn the perceptual comparison mode on. See below for more information. -
gammaGamma correction for all colors (will be used as base) (default: none) – Any value here will turn the perceptual comparison mode on -
gammaRGamma correction for red – Any value here will turn the perceptual comparison mode on -
gammaGGamma correction for green – Any value here will turn the perceptual comparison mode on -
gammaBGamma correction for blue – Any value here will turn the perceptual comparison mode on
Example:
var firstImage = PNGImage.readImage('path/to/first/image', function (err) {
if (err) {
throw err;
}
var diff = new BlinkDiff({
imageA: srcImage, // Use already loaded image for first image
imageBPath: 'path/to/second/image', // Use file-path to select image
delta: 50, // Make comparison more tolerant
outputMaskRed: 0,
outputMaskBlue: 255, // Use blue for highlighting differences
hideShift: true, // Hide anti-aliasing differences - will still determine but not showing it
imageOutputPath: 'path/to/output/image'
});
diff.run(function (error, result) {
if (error) {
throw error;
} else {
console.log(diff.hasPassed(result.code) ? 'Passed' : 'Failed');
console.log('Found ' + result.differences + ' differences.');
}
});
});
####Cropping
Images can be cropped before they are compared by using the cropImageA or cropImageB parameters. Single values can be left off, and the system will calculate the correct dimensions. However, x/y coordinates have priority over width/height as the position are usually more important than the dimensions – image will also be clipped by the system when needed.
####Perceptual Comparison
The perceptual comparison mode considers the perception of…