How to Compare Two Images Using Node.js

How to Compare Two Images Using Node.js

21 May 2024 Stephan Petzl Leave a comment Tech-Help

Comparing images programmatically can be essential for tasks such as visual regression testing, detecting changes, or validating image processing outputs. This guide will walk you through several methods to compare two images using Node.js, providing practical examples and highlighting the most effective libraries.

Using Node-OpenCV

The node-opencv module is a comprehensive library that leverages OpenCV, a powerful image processing library. It allows for advanced image comparison operations:

const cv = require('opencv');

cv.readImage('image1.png', function (err, img1) {
  if (err) throw err;

  cv.readImage('image2.png', function (err, img2) {
    if (err) throw err;

    const diff = new cv.Matrix();
    diff.absDiff(img1, img2);
    const similarity = diff.countNonZero();
    console.log(`Similarity: ${similarity}`);
  });
});

This method is robust and suitable for complex image processing tasks. However, it requires OpenCV to be installed on your system, which might not be ideal for all environments.

Using Pixelmatch

For a more lightweight and straightforward approach, consider using pixelmatch. This library is specifically designed for pixel-level image comparison and is easy to integrate:

const fs = require('fs');
const PNG = require('pngjs').PNG;
const pixelmatch = require('pixelmatch');

const img1 = PNG.sync.read(fs.readFileSync('img1.png'));
const img2 = PNG.sync.read(fs.readFileSync('img2.png'));
const {width, height} = img1;
const diff = new PNG({width, height});

const difference = pixelmatch(img1.data, img2.data, diff.data, width, height, {threshold: 0.1});

fs.writeFileSync('diff.png', PNG.sync.write(diff));
console.log(`${difference} pixels differ`);

Pixelmatch is efficient, with no external dependencies, and suitable for both Node.js and browser environments. It provides accurate anti-aliased pixel detection and perceptual color difference metrics.

Other Alternatives

While the above methods are highly recommended, other libraries like looks-same and js-imagediff also offer image comparison functionalities. However, they may not be as actively maintained or feature-rich as pixelmatch.

Conclusion

Choosing the right tool for image comparison in Node.js depends on your specific needs and environment. For robust image processing, node-opencv is a powerful choice. For straightforward, efficient, and dependency-free comparison, pixelmatch stands out as the best option.

Enhancing Image Comparison with Repeato

For developers and testers looking to streamline their app testing processes, Repeato, a no-code test automation tool for iOS and Android, offers a compelling solution. Repeato allows you to create, run, and maintain automated tests with ease, leveraging computer vision and AI for accurate image recognition. This can be particularly useful for visual regression testing, ensuring your app’s UI remains consistent across updates. With Repeato’s intuitive test recorder and scripting interface, both novice and advanced testers can automate complex use cases efficiently.

Explore more about Repeato and its capabilities on our blog or get started with our comprehensive documentation.

Like this article? there’s more where that came from!