Error when comparing two images with the same sizes
See original GitHub issueHello, 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:

Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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
https://stackoverflow.com/questions/18510897/how-to-compare-two-images-using-node-js- This works fine,you can try this