-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_img.js
34 lines (29 loc) · 959 Bytes
/
util_img.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const pixelmatch = require('pixelmatch');
// 0.1 means 10% of the pixels can differ and the images are still equal
const IS_EQUAL_THRESHOLD = 0.1;
/**
* Compares 2 images by counting the no of differing pixels
* @param {*} img1
* @param {*} img2
*/
const isEqual = (img1, img2) => {
// Images with different sizes are not equal
if (img1.height !== img2.height) return false;
if (img1.width !== img2.width) return false;
const diffPixels = pixelmatch(img1.data, img2.data, diff.data, img1.width, img1.height, {
threshold: 0.1, // That's the default threshold
});
const totalPixels = img1.width * img1.height;
const maxDiffPixels = totalPixels * IS_EQUAL_THRESHOLD;
return (diffPixels < maxDiffPixels);
};
const imageArrayIncludesImage = (imageArray, img) => {
for (i = 0; i < imageArray.length; i++) {
if (isEqual(img, imageArray[i])) return true;
}
return false;
};
exports = {
isEqual,
imageArrayIncludesImage,
};