diff --git a/src/utils/matchImages.ts b/src/utils/matchImages.ts index c7c6d0f..690d782 100644 --- a/src/utils/matchImages.ts +++ b/src/utils/matchImages.ts @@ -1,14 +1,22 @@ import fs from "fs"; +import path from "path"; import { PNG } from "pngjs"; import pixelmatch from "pixelmatch"; -export const matchImages = (img1Path: string, img2Path: string) => { - const img1 = PNG.sync.read(fs.readFileSync(img1Path)); - const img2 = PNG.sync.read(fs.readFileSync(img2Path)); +export const matchImages = (img1Buff: Buffer, img2Buff: Buffer) => { + const img1 = PNG.sync.read(img1Buff); + const img2 = PNG.sync.read(img2Buff); const { width, height } = img1; + 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, }); + + fs.writeFileSync( + path.resolve(process.cwd(), "diff", `${out}.png`), + diff.data + ); + return out; };