pHash()
Compute the perceptual hash of the image as a binary string.
From @jimp/plugin-hash
pHash computes a perceptual hash: a short fingerprint of what the image looks like, as a string of 0s and 1s.
Images that look alike get hashes that differ in only a few bits, even after resizing, recompressing or a change in brightness, so you can use it to find duplicates and near-duplicates.
Signature
image.pHash(options?: { size?: number; smallerSize?: number }): stringpHash doesn’t change the image. It returns a string of smallerSize * smallerSize binary digits, 64 by default.
Options
| Option | Type | Default | Description |
|---|---|---|---|
size |
number |
32 |
Width and height the image is scaled to before the transform. A whole number from 2 to 256, at least smallerSize. |
smallerSize |
number |
8 |
Width and height of the block of low frequencies the hash is made from. A whole number from 2 to 64. |
Example
import { Jimp, compareHashes } from "jimp";
const original = await Jimp.read("photo.jpg");const thumbnail = await Jimp.read("photo-thumbnail.jpg");
const a = original.pHash(); // "1011111000011111110010111110000011100000011000000001111010010010"const b = thumbnail.pHash();
compareHashes(a, b); // 0 for identical hashes, about 0.5 for unrelated imagesStore the hashes, for example in a database, and compare new images against them with compareHashes or distanceFromHash without keeping the images around.
A longer hash
A larger smallerSize keeps more detail, so it tells similar images apart more finely. The hash is smallerSize * smallerSize bits long:
image.pHash({ smallerSize: 16 }).length; // 256image.pHash({ size: 64, smallerSize: 16 }).length; // 256, from a 64x64 copyOnly compare hashes made with the same options.
How it works
- Transparent pixels are flattened onto mid-gray (
128), so transparency is handled the same way for every image. - The image is resized to
size×sizeand converted to grayscale. - A discrete cosine transform (DCT) splits it into frequencies, and the top-left
smallerSize×smallerSizeblock, the lowest frequencies, is kept. - Each of those values becomes a
1if it’s above their average (not counting the first, overall-brightness term) and0otherwise.
Because only the coarse structure survives, the hash ignores small changes. It isn’t designed to survive changes to that structure: a flipped, rotated or cropped image usually gets a very different hash.
Notes
- Doesn’t modify the image. The hash is computed from a copy.
- Same as
hash(2).hashreturns the same bits in a shorter base, 64 by default. - Validation. Options are checked with zod.
{ size: 1 }fails withToo small: expected number to be >=2, a fraction fails withInvalid input: expected int, received number, and asmallerSizeabovesizefails withsmallerSize must not be larger than size. - Speed. Larger options cost more. On an 800 × 600 image the default hash took about 12 ms in our tests, and
{ size: 256, smallerSize: 64 }about 200 ms. - Added in 2.0: the
sizeandsmallerSizeoptions. Earlier versions always usesize: 32andsmallerSize: 8. - Changed in 2.0: the average used to include the first, overall-brightness term, which made bright, low-contrast images such as sketches all hash alike, and transparent pixels were hashed by whatever color was hidden under them. Every hash, opaque images’ included, differs from 1.x, so hash stored images again.
greyscale’s rounding change in 2.0 doesn’t affect the hash.