🔣 Bitmaps matcher

This commit is contained in:
ful1e5 2020-08-29 10:30:32 +05:30
parent 7b7d5f7ae0
commit 9c22d7c5af

View file

@ -1,14 +1,22 @@
import fs from "fs"; import fs from "fs";
import path from "path";
import { PNG } from "pngjs"; import { PNG } from "pngjs";
import pixelmatch from "pixelmatch"; import pixelmatch from "pixelmatch";
export const matchImages = (img1Path: string, img2Path: string) => { export const matchImages = (img1Buff: Buffer, img2Buff: Buffer) => {
const img1 = PNG.sync.read(fs.readFileSync(img1Path)); const img1 = PNG.sync.read(img1Buff);
const img2 = PNG.sync.read(fs.readFileSync(img2Path)); const img2 = PNG.sync.read(img2Buff);
const { width, height } = img1; const { width, height } = img1;
const diff = new PNG({ width, height }); const diff = new PNG({ width, height });
return pixelmatch(img1.data, img2.data, diff.data, width, height, { const out = pixelmatch(img1.data, img2.data, diff.data, width, height, {
threshold: 0.3, threshold: 0.3,
}); });
fs.writeFileSync(
path.resolve(process.cwd(), "diff", `${out}.png`),
diff.data
);
return out;
}; };