distance tells you how different two images look, from 0 (the same) to about 0.5 (unrelated).
It compares their perceptual hashes, so it isn’t thrown off by size, compression or small color changes the way a pixel-by-pixel diff is.
Signature
import { distance } from "jimp";
distance( img1: JimpInstance, img2: JimpInstance, options?: { size?: number; smallerSize?: number },): numberdistance is a function exported from jimp, not an image method. It doesn’t change either image.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
img1 |
Jimp |
required | The first image. |
img2 |
Jimp |
required | The second image. It can be a different size from the first. |
options |
object |
{} |
size and smallerSize for both hashes. See pHash. |
Example
import { Jimp, distance } from "jimp";
const photo = await Jimp.read("photo.jpg");
const smaller = photo.clone().resize({ w: 200 }).brightness(1.2);const flipped = photo.clone().flip({ horizontal: true });
distance(photo, photo.clone()); // 0distance(photo, smaller); // small, e.g. 0.03125: 2 of 64 bits differdistance(photo, flipped); // large, e.g. 0.453125: looks like a different imageCompare more finely
A longer hash notices smaller differences:
distance(photo, smaller, { smallerSize: 16 }); // e.g. 0.04296875: 11 of 256 bitsNotes
- What it computes.
distance(a, b)iscompareHashes(a.pHash(), b.pHash()): the share of hash bits that differ (the normalized Hamming distance). - Picking a threshold. Values below about
0.15usually mean the same picture. Test with your own images to choose a cutoff. - What it ignores, and what it doesn’t. Resizing, JPEG compression and brightness or contrast changes move the distance very little. Flips, rotations, crops and large edits change the structure the hash is built from, so they look like different images.
- Compare many images? Hash each image once with
pHashorhashand compare the strings withcompareHashes, instead of callingdistancefor every pair. - Added in 2.0: the
optionsargument. - Changed in 2.0: the hash is computed differently (see
pHash), so distances can differ from 1.x. - Pixel-exact comparisons. To find out which pixels changed, for example in visual tests, use
diff.