Alternative JavaScript image hashing library.
npm i @alttiri/image-hash
The library to generate a perceptual hash for ImageData
input.
Supported perceptual hashes:
dHash
(difference hash)aHash
(average hash)mHash
(median hash)bHash
(block hash)import {dHash} from "@alttiri/image-hash";
const imagePath = "alyson_hannigan_500x500.jpg";
const imageData = await getImageData(imagePath);
const hash = dHash(imageData);
console.log(hash.hex); // "8f94b43434245452"
const hash1 = dHash(await getImageData("kittens-3264x2448.jpg"));
const hash2 = dHash(await getImageData("kittens-960x720.jpg"));
console.log(hash1.hex); // "e020acce864cae8a"
console.log(hash2.hex); // "e020acce864cae8a"
console.log(hash1.diff(hash2)); // 0
ImageData
?The reasonable question is how to get ImageData
.
On Node.js I recommend to use sharp
library.
On browsers get ImageData
with OffscreenCanvas
.
It’s only ~20 lines of code (node, browser).
But for convenience I created a library @alttiri/get-image-data
:
npm i @alttiri/get-image-data
Also, for Node.js application do not forget to install sharp
dependency:
npm i sharp
Then just import it:
import {getImageDataWithSharp as getImageData} from "@alttiri/get-image-data";
import {getImageDataWithCanvas as getImageData} from "@alttiri/get-image-data";
npm i @alttiri/image-hash @alttiri/get-image-data sharp
import {getImageDataWithSharp as getImageData} from "@alttiri/get-image-data";
import {dHash} from "@alttiri/image-hash";
const imagePath = "alyson_hannigan_500x500.jpg";
const imageData = await getImageData(imagePath);
const hash = dHash(imageData);
console.log(hash.hex); // "8f94b43434245452"