mapbox / pixelmatch

The smallest, simplest and fastest JavaScript pixel-level image comparison library
ISC License
6.15k stars 305 forks source link

The problem is me or the lib? #115

Closed hiwyn closed 2 years ago

hiwyn commented 2 years ago

Trying to test the lib, but got some error

Test script:

import fs from "fs";
import pixelmatch from "pixelmatch";
import { PNG } from "pngjs";

async function CompareTwoImages1() {
  const img1 = PNG.sync.read(fs.readFileSync("mace.png"));
  const img2 = PNG.sync.read(fs.readFileSync("cropped.png"));
  const numDiffPixels = pixelmatch(img1, img2, null, 32, 32, {
    threshold: 0.1,
  });
  console.log(numDiffPixels());
}
CompareTwoImages1();

Error:

(node:4982) UnhandledPromiseRejectionWarning: Error: unrecognised content at end of stream
    at module.exports.SyncReader.process (/home/hiwyn/playimg/node_modules/pngjs/lib/sync-reader.js:43:11)
    at module.exports (/home/hiwyn/playimg/node_modules/pngjs/lib/parser-sync.js:68:10)
    at Object.exports.read (/home/hiwyn/playimg/node_modules/pngjs/lib/png-sync.js:7:10)
    at CompareTwoImages1 (file:///home/hiwyn/playimg/test.js:6:25)
    at file:///home/hiwyn/playimg/test.js:13:1
    at ModuleJob.run (internal/modules/esm/module_job.js:145:37)
    at async Loader.import (internal/modules/esm/loader.js:182:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)
(node:4982) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:4982) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
mourner commented 2 years ago

pixelmatch accepts raw arrays (e.g. img1.data) as input rather than image objects returned by PNG.sync.read.

hiwyn commented 2 years ago

Ty!