Resemble.js

Image analysis and comparison

README

Low-maintenance mode: Two to four times a year the library author will spend some time to keep the library useful. Feel free to raise issues and contribute improvements, but please be aware that it may be sometime before a response is given.


Resemble.js

Build Status Coverage Code Health MIT License NPM Downloads

Analyse and compare images with Javascript and HTML5. More info & Resemble.js Demo. Compatible with Node.js >8.



Get it


npm install resemblejs

Node.js


Resemble.js uses node-canvas for Node.js support. This is a pre-built dependency that may fail in certain environments. If you are using Resemble.js for in-browser analysis only, you can skip the node-canvas dependency withnpm install --no-optional. If you need Node.js support and the Canvas dependency fails, try using a previous version of Resemble.js, or npm install --build-from-source.

Example


Retrieve basic analysis on an image:

  1. ``` js
  2. var api = resemble(fileData).onComplete(function (data) {
  3.     console.log(data);
  4.     /*
  5. {
  6.    red: 255,
  7.    green: 255,
  8.    blue: 255,
  9.    brightness: 255
  10. }
  11. */
  12. });
  13. ```

Use resemble to compare two images:

  1. ``` js
  2. var diff = resemble(file)
  3.     .compareTo(file2)
  4.     .ignoreColors()
  5.     .onComplete(function (data) {
  6.         console.log(data);
  7.     });
  8. ```

Scale second image to dimensions of the first one:

  1. ``` js
  2. diff.scaleToSameSize();
  3. ```

You can also change the comparison method after the first analysis:

  1. ``` js
  2. diff.ignoreAntialiasing();
  3. ```

And change the output display style:

  1. ``` js
  2. resemble.outputSettings({
  3.     errorColor: {
  4.         red: 255,
  5.         green: 0,
  6.         blue: 255
  7.     },
  8.     errorType: "movement",
  9.     transparency: 0.3,
  10.     largeImageThreshold: 1200,
  11.     useCrossOrigin: false,
  12.     outputDiff: true
  13. });
  14. // .repaint();
  15. ```

Note: resemble.outputSettings mutates global state, effecting all subsequent call to Resemble.


It is possible to narrow down the area of comparison, by specifying a bounding box measured in pixels from the top left:

  1. ``` js
  2. const box = {
  3.     left: 100,
  4.     top: 200,
  5.     right: 200,
  6.     bottom: 600
  7. };
  8. resemble.outputSettings({ boundingBox: box });
  9. ```

  1. ``` js
  2. resemble.outputSettings({ boundingBoxes: [box1, box2] });
  3. ```

You can also exclude part of the image from comparison, by specifying the excluded area in pixels from the top left:

  1. ``` js
  2. const box = {
  3.     left: 100,
  4.     top: 200,
  5.     right: 200,
  6.     bottom: 600
  7. };
  8. resemble.outputSettings({ ignoredBox: box });
  9. ```

  1. ``` js
  2. resemble.outputSettings({ ignoredBoxes: [box1, box2] });
  3. ```

Another way to exclude parts of the image from comparison, is using the ignoreAreasColoredWith option.
Any pixels that match the specified color on a reference image will be excluded from comparison:

  1. ``` js
  2. const color = {
  3.     r: 255,
  4.     g: 0,
  5.     b: 0,
  6.     a: 255
  7. };
  8. resemble.outputSettings({ ignoreAreasColoredWith: color });
  9. ```

By default, the comparison algorithm skips pixels when the image width or height is larger than 1200 pixels. This is there to mitigate performance issues.

You can modify this behaviour by setting the largeImageThreshold option to a different value. Set it to 0 to switch it off completely.

Resemble.js also supports Data URIs as strings:

  1. ``` js
  2. resemble.outputSettings({ useCrossOrigin: false });
  3. var diff = resemble("data:image/jpeg;base64,/9j/4AAQSkZJRgAB...").compareTo("data:image/jpeg;base64,/9j/,/9j/4AAQSkZJRg...");
  4. ```

useCrossOrigin is true by default, you might need to set it to false if you're using Data URIs.

If you'd like resemble to return early:

  1. ``` js
  2. resemble(img1)
  3.     .compareTo(img2)
  4.     .setReturnEarlyThreshold(8)
  5.     .onComplete((data) => {
  6.         /* do something */
  7.     });
  8. ```

Single callback api


The resemble.compare API provides a convenience function that is used as follows:

  1. ``` js
  2. const compare = require("resemblejs").compare;

  3. function getDiff() {
  4.     const options = {
  5.         returnEarlyThreshold: 5
  6.     };

  7.     compare(image1, image2, options, function (err, data) {
  8.         if (err) {
  9.             console.log("An error!");
  10.         } else {
  11.             console.log(data);
  12.         }
  13.     });
  14. }
  15. ```

Node.js


Usage


The API under Node is the same as on the resemble.compare but promise based:

  1. ``` js
  2. const compareImages = require("resemblejs/compareImages");
  3. const fs = require("mz/fs");

  4. async function getDiff() {
  5.     const options = {
  6.         output: {
  7.             errorColor: {
  8.                 red: 255,
  9.                 green: 0,
  10.                 blue: 255
  11.             },
  12.             errorType: "movement",
  13.             transparency: 0.3,
  14.             largeImageThreshold: 1200,
  15.             useCrossOrigin: false,
  16.             outputDiff: true
  17.         },
  18.         scaleToSameSize: true,
  19.         ignore: "antialiasing"
  20.     };

  21.     const data = await compareImages(await fs.readFile("./your-image-path/People.jpg"), await fs.readFile("./your-image-path/People2.jpg"), options);

  22.     await fs.writeFile("./output.png", data.getBuffer());
  23. }

  24. getDiff();
  25. ```

Tests


To run the tests on Node (using Jest), type:

  1. ``` sh
  2. npm run test
  3. ```

There are also some in-browser tests. To run these install and run a http-server such as http-server from the root of the project. Then in the browser, navigate tolocalhost:8080/chai-tests/test.html, open up the developer console to see the results.

Dockerfile


For convenience I've added a simple Dockerfile to run the NodeJS tests in an Ubuntu container

  1. ``` sh
  2. docker build -t rsmbl/resemble .
  3. docker run rsmbl/resemble
  4. ```

Reference to academic papers


As people have asked in the past, Resemble.js hasn't knowingly implemented any published ideas. RGBA colour comparison is simple and straightforward when working with the Canvas API. The antialiasing algorithm was developed at Huddle over several days of trial-and-error using various false-positive results from PhantomCSS tests.