Skip to main content
Deno 2 is finally here πŸŽ‰οΈ
Learn more

dhash

Perceptual hashing is the use of a fingerprinting algorithm that produces a 
snippet or fingerprint of various forms of multimedia.

A dhash implementation for Deno.

Based on the β€œKind of Like That” article by Dr. Neal Krawetz.

Usage

You can compare dhash values by simply computing the Hamming distance between them:

  • A distance of 0 represents an identical, or very similar image
  • A distance greater than 10 means that you’re most likely dealing with a different image
  • A distance between 1 and 10 may indicate that you’re dealing with variations of the same base image
const [hash1, hash2] = await Promise.all([
  dhash("./tests/dalle.png"),
  dhash("./tests/dalle-copyright.png"),
]);

console.log(compare(hash1, hash2));

There are also two functions that you may use to display the fingerprint in a non-hash form:

/**
     *  toAscii will return the fingerprint represented as a matrix of
     *  black/white pixels, represented by default through unicode low density
     *  and full blocks, ie:
     *
     *   β–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘
     *   β–‘β–‘β–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆ
     *   β–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆ
     *   β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆ
     *   β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆ
     *   β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘
     *   β–‘β–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘
     *   β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘
     * /
    toAscii(hash: string, chars: [string, string]): string

    /**
     * save will render the fingerprint as an 8x8px PNG file with black and
     * white pixels, at the specified path.
     *
     * async save(hash: string, file: string): Promise<void>
     */

License

MIT Β© Claudiu Ceia