Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

png-diff

chenglou710MIT0.3.3

Small PNG diff utility, written in pure JS for Node.

png, diff, compare, image

readme

PNG Diff

Small PNG diff utility, written in pure JS for Node.

npm install png-diff

Usage

(Check out the example folder.)

Both methods take two image paths/streams/buffers as input.

var fs = require('fs');
var PNGDiff = require('png-diff');

var image2Stream = fs.createReadStream('2.png');
PNGDiff.outputDiff('1.png', image2Stream, 'diffOutput.png', function(err, diffMetric) {
  if (err) throw err;
  // returns 0 if every pixel's the same; return 1 otherwise. Currently, these
  // are the only two possible metric values; possibility to tweak them in the
  // future
  console.log(diffMetric === 1 ? 'Difference detected.' : 'No difference');
  // highlights the difference in red
  console.log('Diff saved!');
});

var image1Buffer = fs.createReadStream('1.png');
PNGDiff.outputDiffStream(image1Buffer, '2.png', function(err, outputStream, diffMetric) {
  if (err) throw err;

  if (diffMetric === 0) {
    console.log('No difference, no need to output diff result.');
    return;
  }
  outputStream.pipe(fs.createWriteStream('diffOutput2.png'));
});

Second format:

var image2Stream = fs.createReadStream('2.png');
// output identical pixels as transparent instead of highlighting the diff
// (default: false)
PNGDiff.outputDiff('1.png', image2Stream, 'diffOutput.png', true , function(err, diffMetric) {
  if (err) throw err;
  console.log(diffMetric === 1 ? 'Difference detected.' : 'No difference');
  // does not highlight the difference, but replaces common pixels with
  // transparent ones
  console.log('Diff saved!');
});

License

MIT.

changelog

Legend:

  • [I]: improvement
  • [F]: fix

0.3.3 (January 26th 2016)

  • [I] New option to output identical pixels as transparent ones.
  • [I] Dependencies bump (pngjs2 has now taken over Npm pngjs name, so we're switching back).

0.3.1 (August 17th 2015)

  • [I] Switch to using pngjs2. No API change.

0.3.0 (March 12th 2014)

  • [F] Deprecate measureDiff. The diffMetric is now the third argument to the outputDiffStream callback and second for outputDiff. The underlying reason is that it was possible to measure streams and then do outputDiff, except by then, the stream would already have been piped by measureDiff, which causes weird bug messages.

0.2.1 (March 9th 2014)

  • [I] All three methods now accept buffers as input too.

0.2.0 (March 8th 2014)

  • [I] New outputDiffStream that gives you back a stream. See README and example folder.
  • [I] All three methods can now accept streams, in addition to the old file path arguments.
  • [I] Proper error messages that are actually of the Error class.

0.1.4 (March 2nd 2014)

  • [F] Fix callback executing before output has been written.
  • [I] The same callback can now receive error, if any.

0.1.3 (February 10th 2014)

  • [I] Show images dimensions when a size mismatch happens.

0.1.1 (December 20th 2013)

  • [I] No more error throwing internally; better async compatibility.

0.1.0 (December 13th 2013)

  • [I] Pass error as first parameter to callback.

0.0.0 (December 13th 2013)

  • Initial public release.