-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
42 lines (37 loc) · 1.14 KB
/
index.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
35
36
37
38
39
40
41
42
const fs = require("fs");
const PNG = require("pngjs").PNG;
const pixelmatch = require("pixelmatch");
const path = require("path");
const sourceImages = path.join(__dirname, "srcImages");
const destImages = path.join(__dirname, "destImages");
const diffImages = "./diff";
fs.readdir(sourceImages, function(err, files) {
if (err) {
return console.log("Unable to scan directory: " + err);
}
files.forEach(function(file) {
if (path.extname(file) === ".png") {
const img1 = PNG.sync.read(fs.readFileSync(`${sourceImages}/${file}`));
const img2 = PNG.sync.read(fs.readFileSync(`${destImages}/${file}`));
const { width, height } = img1;
const diff = new PNG({ width, height });
const difference = pixelmatch(
img1.data,
img2.data,
diff.data,
width,
height,
{
threshold: 0
}
);
if (difference) {
if (!fs.existsSync(diffImages)) {
fs.mkdirSync(diffImages);
}
fs.writeFileSync(`${diffImages}/${file}`, PNG.sync.write(diff));
}
console.log(`${file}: ${difference} pixels different`);
}
});
});