npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-image-diff

v0.3.0

Published

Image diff'ing tool that compares pixel by pixel.

Downloads

15

Readme

npm version

gulp-image-diff

gulp-image-diff, a gulp plugin, is a image diff'ing tool. We compare pixel by pixel with an option for color tolerance.

We also emit the raw data and percentage of disparity in analysis, see the What we emit section.

Install

Latest Version: 0.3.0

Currently on GitHub.

npm install MadLittleMods/gulp-image-diff

Usage

var gulp = require('gulp');
var imageDiff = require('gulp-image-diff');

gulp.task('diff-images', function() {
	return gulp.src(['my-image.png'])
		.pipe(imageDiff({
			referenceImage: 'reference.png',
			// Equivalent to 10% tolerance between reference and compare pixels
			pixelColorTolerance: 0.10,
			differenceMapImage: './diff.png'
		}));
});

Chaining

Since gulp-image-diff is a pass-through and emits an untouched image, you can pipe it multiple times into the diff against different references.

var gulp = require('gulp');
var imageDiff = require('gulp-image-diff');

gulp.task('diff-images', function() {
	return gulp.src(['my-image.png'])
		.pipe(imageDiff({
			referenceImage: 'reference1.png',
			differenceMapImage: './diff1.png'
		}))
		.pipe(imageDiff({
			referenceImage: 'reference2.png',
			differenceMapImage: './diff2.png'
		}))
		.pipe(imageDiff({
			referenceImage: 'reference3.png',
			differenceMapImage: './diff3.png'
		}));
});

Logging

You can use the logProgress setting to output some analysis info every time a diff completes.

var gulp = require('gulp');
var imageDiff = require('gulp-image-diff');

gulp.task('diff-images', function() {
	return gulp.src(['my-image.png'])
		.pipe(imageDiff({
			referenceImage: 'reference.png',
			logProgress: true
		}));
});

You can also hook onto .on('log', ...) events which are emitted no matter what.

var gulp = require('gulp');
var imageDiff = require('gulp-image-diff');

gulp.task('diff-images', function() {
	return gulp.src(['my-image.png'])
		.pipe(imageDiff({
			referenceImage: 'reference.png'
		}))
		.on('log', function(message) {
			console.log(message);
		})
		// We are just showing that you can do more below the `.on('log', ...)`.
		// Saving it out the analysis to JSON (see example below for more info)
		.pipe(imageDiff.jsonReporter())
		.pipe(gulp.dest('./diff-analysis-report.json'));
});

Reporting/Logging/Generating JSON

gulp-image-diff includes imageDiff.jsonReporter() which you can use to pipe the diff analysis into and generate a JSON file.

Since we chain the analysis, the JSON reporter will be able to pick up all analysis in the consecutive chain.

var gulp = require('gulp');
var imageDiff = require('gulp-image-diff');

gulp.task('diff-images', function() {
	return gulp.src(['icon-i.png', 'icon-o.png', 'icon-c.png'])
		.pipe(imageDiff({
			referenceImage: 'test-images/icon-reference.png',
			differenceMapImage: './diff1.png'
		}))
		.pipe(imageDiff({
			referenceImage: 'test-images/icon-reference2.png',
			differenceMapImage: './diff2.png'
		}))
		.pipe(imageDiff.jsonReporter())
		.pipe(gulp.dest('./diff-analysis-report.json'));
});

Options

  • options: object - hash of options
    • referenceImage: string|buffer|vinyl-file - Pass in the image and we can take of it
      • Default: null
    • pixelColorTolerance: number - 0-1 representing the allowed color difference between the referenc and compare pixels
      • Default: 0.01
      • 0 means no tolerance. Pixels need to be exactly the same
      • This option allows for slight differences in aliasing in for example text
    • differenceMapImage: string|function - Pass a string path+filename or a function that returns string path+filename of where to save the difference image
      • Default: null
      • If function the compare and reference paths are provided function(referencePath, comparePath). Your function should return a string path+filename
      • function(referencePath, compareImagePath): Return string path+filename
    • differenceMapColor: object - The color for each pixel used in the differenceMapImage that is not within tolerance.
      • Default: { r: 255, g: 0, b: 0, a: 200 }
      • If transparent, it will be alpha-blended with the reference image.
    • logProgress: bool - Log each diff as it completes. Prints out some of the analysis.
      • You can also hook onto .on('log', ...) events which are emitted no matter what.

Notes:

  • We use rgba 0-255 range colors.
  • The reference image is the master and we iterate over the reference image pixels. If the compare image doesn't have a pixel at that coordinate, we just skip to the next(does not increment the difference counter).
    • This is especially important point to know if you are comparing different size images.

What we emit

gulp-image-diff is a pass-through and will emit the original image as a normal Gulp vinyl file.

We also attach an analysis containing the raw data of differences and differenceMap which is a buffer of the difference image in case you want to consume it later down the pipe.

  • Gulp vinyl file. We emit whatever you passed in (untouched).
    • analysis: object - hash of data that is chained through out multiple diff calls
      • differences: number - compareResult.numDifferences,
      • total: number - total amount of pixels in the reference image,
      • disparity: number - 0-1 percentage value. This is a just a shortcut for differences/total
      • referenceImage: string - path of the reference image used in the diff
      • compareImage: string - path of the compare image used in the diff
      • *differenceMap: string - path to the difference map image, only available if saved successfully
    • differenceMap: buffer - The difference png image.

Events

.on('log', function(message) { })

See Logging example for a nice code example.

The message is the same message outputted to the console when logProgress is true.

.on('error', function(err) { })

A normal gulp error. There are a variety of errors. See source code for more details.

Testing

We have a series of unit tests. We use Mocha.

Install Mocha globally:

npm install -g mocha

Run tests with: mocha or npm test