From 687a5f97e27947501978b5f714198c9f70e67823 Mon Sep 17 00:00:00 2001 From: ful1e5 <24286590+ful1e5@users.noreply.github.com> Date: Fri, 28 Aug 2020 20:37:59 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=B3=20Pixel=20Matching=20utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/matchImages.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/utils/matchImages.ts diff --git a/src/utils/matchImages.ts b/src/utils/matchImages.ts new file mode 100644 index 0000000..c7c6d0f --- /dev/null +++ b/src/utils/matchImages.ts @@ -0,0 +1,14 @@ +import fs from "fs"; +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)); + const { width, height } = img1; + const diff = new PNG({ width, height }); + + return pixelmatch(img1.data, img2.data, diff.data, width, height, { + threshold: 0.3, + }); +};