From 9c22d7c5afddd6cb17ff1c61d3ed84a29655167e Mon Sep 17 00:00:00 2001 From: ful1e5 <24286590+ful1e5@users.noreply.github.com> Date: Sat, 29 Aug 2020 10:30:32 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A3=20Bitmaps=20matcher?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/matchImages.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) 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; };