question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when comparing two images with the same sizes

See original GitHub issue

Hello, here is the code I tested:

import { promises as fs } from 'fs';
import pixelmatch from 'pixelmatch';
import sizeOf from 'buffer-image-size';

const img1 = './public/images/20190605133954-1-atoms.Tables.ATOM-038 - TableCell.png';
const img2 = './public/images/20190605133954-1-atoms.Tables.ATOM-037 - TableHead.png';

const promise1 = fs.readFile(img1);
const promise2 = fs.readFile(img2);

Promise.all([ promise1, promise2 ])
  .then(([ buffer1, buffer2 ]) => {
    const { width: width1, height: height1 } = sizeOf(buffer1);
    const { width: width2, height: height2 } = sizeOf(buffer2);
    if (width1 === width2 && height1 === height2) {
      const diff = pixelmatch(buffer1, buffer2, null, width1, height1, { threshold: 0 });
    }
  })
  .catch(error => console.error(error));

And I got the following error:

Error: Image sizes do not match.
    at pixelmatch (/home/tocab/Projects/Smile/diplopia/diplopia-api/node_modules/pixelmatch/index.js:19:15)
    at file:///home/tocab/Projects/Smile/diplopia/diplopia-api/test.mjs:17:20

I used node v12.4.0 with the --experimental-modules flag.

In your lib you are testing img1.length !== img2.length and this is false when using Buffers although the images have the same dimensions (800x600).

Here are the two images I used for my test: 20190605133954-1-atoms Tables ATOM-037 - TableHead 20190605133954-1-atoms Tables ATOM-038 - TableCell

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
BlazeGawlikcommented, Dec 29, 2021

I turned to Jimp: https://www.npmjs.com/package/jimp

const jimage1 = await Jimp.read(imgBuffer); const jimage2 = await Jimp.read(imgUrl); const width = jimage1.bitmap.width; var diff = Jimp.diff(jimage1, jimage2, .1); console.log("Percent Difference - ", diff.percent);

It’s effectively a wrapper for pixel-match

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting error while comparing two images using opencv in ...
Check if both are RGB or GRAY, and resize one of them to the other size. An only resize is required. Share.
Read more >
Compute the difference between two images but with different ...
What do you want to measure? IM compares corresponding pixels in the two images. The obvious method would resize one to the size...
Read more >
How-To: Python Compare Two Images - PyImageSearch
In this blog post I'll show you how to use Python to compare two images using Mean Squared Error and Structural Similarity Index....
Read more >
How do I compare two images? - MATLAB Answers - MathWorks
I am looking for a method, to compare the two images, so I can estimate the accuracy of the automated segmentation. How can...
Read more >
Comparing Images With idiff - OpenImageIO 2.4 - Read the Docs
The idiff program compares two images, printing a report about how different they are and optionally producing a third image that records the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found