Comparing
compareHashes()
Compare two perceptual hashes from pHash() or hash().
From @jimp/plugin-hash
compareHashes compares two hash strings you already have and returns how different they are, from 0 to 1.
It’s the fast way to compare many images: hash each one once with pHash or hash, then compare the strings.
Signature
import { compareHashes } from "jimp";
compareHashes(hash1: string, hash2: string, base?: number): numbercompareHashes is a function exported from jimp. It decodes both hashes to bits and returns the share of bits that differ.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
hash1 |
string |
required | A hash from pHash() or hash(). |
hash2 |
string |
required | A hash made the same way: same options and base. |
base |
number |
2 or 64 |
The base the hashes are written in, from 2 to 64. Defaults to 2 when both hashes are binary strings, as from pHash(), and to 64 otherwise, the default of hash(). |
Example
Find near-duplicates in a folder of images:
import { Jimp, compareHashes } from "jimp";
const files = ["a.jpg", "b.jpg", "c.png", "d.png"];const hashes = [];
for (const file of files) { const image = await Jimp.read(file); hashes.push({ file, hash: image.pHash() });}
for (let i = 0; i < hashes.length; i++) { for (let j = i + 1; j < hashes.length; j++) { const d = compareHashes(hashes[i].hash, hashes[j].hash);
if (d < 0.15) { console.log(`${hashes[i].file} and ${hashes[j].file} look alike (${d})`); } }}Each image is decoded and hashed once, and the comparisons are just string checks.
Compare stored hash() strings
Hashes from hash() are shorter to store, and compare the same way. Pass the base if it isn’t the default 64:
// The same result as compareHashes(a.pHash(), b.pHash())compareHashes(a.hash(), b.hash());
// Hashes in base 16compareHashes(a.hash(16), b.hash(16), 16);Notes
- Reading the result.
0means identical hashes. Unrelated images land around0.5. Values below about0.15usually mean the same picture. - Same as
distance. For hashes made with the same options,compareHashes(a.hash(), b.hash())andcompareHashes(a.pHash(), b.pHash())both equaldistance(a, b). - Wrong base. A hash with characters that don’t exist in the base throws, for example
"be1fcbe0e0601e92" isn't a base 64 hash. Pass the base the hashes were made with to compareHashes, e.g. compareHashes(hash1, hash2, 16).Passbasefor anyhash()base other than64. Abaseoutside2to64throwsbase must be a number between 2 and 64. - Lengths must match. Hashes of different lengths throw
Can't compare hashes of different lengths (64 and 256). Both hashes must be made with the same size and smallerSize options (and, for hash(), the same base).Hashes of the same length made with different options don’t throw, but comparing them is meaningless. - Related helpers.
distancehashes two images and compares them in one call, anddistanceFromHashcompares an image with a storedpHash()hash. - Added in 2.0: the
baseargument. - Changed in 2.0:
hash()strings used to be compared character by character, so their distances came out larger and coarser than for the same images’pHash()strings. They’re now decoded to bits first, and a string that isn’t a valid hash in the base throws.