distanceFromHash hashes the image with pHash and tells you how far that hash is from one you already have.
Use it to check a new image against hashes you’ve stored, without loading the images they came from.
Signature
image.distanceFromHash( compareHash: string, options?: { size?: number; smallerSize?: number },): numberdistanceFromHash doesn’t change the image. It returns a number from 0 to 1: the share of bits that differ.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
compareHash |
string |
required | A hash from pHash() (or hash(2)). |
options |
object |
{} |
The size and smallerSize that compareHash was made with. See pHash. |
Example
import { Jimp } from "jimp";
// Hashes saved earlier with image.pHash()const known = { logo: "1011111000011111110010111110000011100000011000000001111010010010",};
const upload = await Jimp.read("upload.png");
if (upload.distanceFromHash(known.logo) < 0.15) { console.log("This looks like the logo");}With a longer hash
If the stored hash was made with options, pass the same options so the image is hashed the same way:
const stored = original.pHash({ smallerSize: 16 });
image.distanceFromHash(stored, { smallerSize: 16 });Notes
- Reading the result.
0means the hashes are identical. Unrelated images land around0.5, because about half their bits match by chance. Where to draw the line depends on your images; values below about0.15usually mean the same picture. - Same as
compareHashes.image.distanceFromHash(hash)gives the same result ascompareHashes(image.pHash(), hash). - Mismatched hashes throw. If
compareHashhas a different length from the image’s hash, because it came fromhash()in another base or used other options,distanceFromHashthrowsCan't compare hashes of different lengths (64 and 11). Both hashes must be made with the same size and smallerSize options (and, for hash(), the same base).To compare with a storedhash()string, usecompareHashes(image.hash(), stored). - Added in 2.0: the
optionsargument. - Changed in 2.0: hashes of different lengths used to give a meaningless distance instead of throwing, and hashes stored with 1.x don’t match 2.0’s (see
pHash).