From af3440fa3a0e1ffec95daa5ffaf25ebb590fbcfe Mon Sep 17 00:00:00 2001 From: ful1e5 <24286590+ful1e5@users.noreply.github.com> Date: Mon, 31 Aug 2020 12:39:41 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=96=BC=20Frame=20Processing=20util?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/common/src/utils/matchImages.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 packages/common/src/utils/matchImages.ts diff --git a/packages/common/src/utils/matchImages.ts b/packages/common/src/utils/matchImages.ts new file mode 100644 index 00000000..f4127d6a --- /dev/null +++ b/packages/common/src/utils/matchImages.ts @@ -0,0 +1,19 @@ +import { PNG } from "pngjs"; +import pixelmatch from "pixelmatch"; + +interface MatchImagesArgs { + img1Buff: Buffer; + img2Buff: Buffer; +} + +export const matchImages = ({ img1Buff, img2Buff }: MatchImagesArgs) => { + 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, { + threshold: 0.25 + }); +};