What is @types/pixelmatch?
@types/pixelmatch provides TypeScript type definitions for the pixelmatch library, which is used for image comparison and finding differences between images.
What are @types/pixelmatch's main functionalities?
Image Comparison
This feature allows you to compare two images and generate a diff image highlighting the differences. The code sample reads two PNG images, compares them using pixelmatch, and writes the resulting diff image to a file.
const pixelmatch = require('pixelmatch');
const fs = require('fs');
const PNG = require('pngjs').PNG;
const img1 = PNG.sync.read(fs.readFileSync('image1.png'));
const img2 = PNG.sync.read(fs.readFileSync('image2.png'));
const {width, height} = img1;
const diff = new PNG({width, height});
const numDiffPixels = pixelmatch(img1.data, img2.data, diff.data, width, height, {threshold: 0.1});
fs.writeFileSync('diff.png', PNG.sync.write(diff));
Custom Threshold
This feature allows you to set a custom threshold for the image comparison. The code sample demonstrates how to set a higher threshold value, which makes the comparison less sensitive to minor differences.
const pixelmatch = require('pixelmatch');
const fs = require('fs');
const PNG = require('pngjs').PNG;
const img1 = PNG.sync.read(fs.readFileSync('image1.png'));
const img2 = PNG.sync.read(fs.readFileSync('image2.png'));
const {width, height} = img1;
const diff = new PNG({width, height});
const numDiffPixels = pixelmatch(img1.data, img2.data, diff.data, width, height, {threshold: 0.5});
fs.writeFileSync('diff.png', PNG.sync.write(diff));
Other packages similar to @types/pixelmatch
resemblejs
Resemble.js is a JavaScript library for image comparison. It provides more advanced features like tolerance settings for colors and antialiasing detection. Compared to pixelmatch, Resemble.js offers a more comprehensive set of options for fine-tuning image comparisons.
looks-same
Looks-same is another image comparison library that focuses on pixel-level comparison. It supports ignoring antialiasing and has options for comparing images with different dimensions. Looks-same is similar to pixelmatch but offers additional flexibility in handling image size differences.
Installation
npm install --save @types/pixelmatch
Summary
This package contains type definitions for pixelmatch (https://github.com/mapbox/pixelmatch#readme).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/pixelmatch.
declare function Pixelmatch(
img1: Buffer | Uint8Array | Uint8ClampedArray,
img2: Buffer | Uint8Array | Uint8ClampedArray,
output: Buffer | Uint8Array | Uint8ClampedArray | null,
width: number,
height: number,
options?: Pixelmatch.PixelmatchOptions,
): number;
declare namespace Pixelmatch {
type RGBTuple = [number, number, number];
interface PixelmatchOptions {
readonly threshold?: number | undefined;
readonly includeAA?: boolean | undefined;
alpha?: number | undefined;
aaColor?: RGBTuple | undefined;
diffColor?: RGBTuple | undefined;
diffColorAlt?: RGBTuple | undefined;
diffMask?: boolean | undefined;
}
}
export = Pixelmatch;
Additional Details
- Last updated: Tue, 07 Nov 2023 09:09:39 GMT
- Dependencies: @types/node
Credits
These definitions were written by Oleg Repin, Piotr Błażejewicz, and Damian Frizzi.