Perceptual JPEG image hashing for Deno via Blockhash.js
import { imageHash, hammingDistance, DecodedImage } from "https://deno.land/x/imagehash/mod.ts";
serve(async (request: Request) => {
const rawImageData: Uint8Array = request.body
const hash: String = await imageHash(rawImageData, 10);
// OR
const decodedImage: DecodedImage = jpeg.decode(rawImageData) // decodedImage = { width: 100, height: 100, data: ... }
const hash: String = await imageHash(decodedImage, 10);
// Hamming Distance
const bitDifference = hammingDistance(hash1, hash2); // returns number
return new Response(JSON.stringify({hash, bitDifference}), {status: 200})
Returns: string
Returns the hash value as a string of hexadecimal characters.
Type: Uint8Array
or DecodedImage
/jpeg.Image
The image data to hash. This can be a Uint8Array
or an object that matches DecodedImage
: {data: `Uint8Array`, width: `number`, height: `number`})
Type: number
/ Default: 10
The number of bits to use for the hash. The higher the number, the more accurate the hash, but the longer it will take to compute.
Calculate distance in bits between two hashes.
This module uses the following projects with some changes to work with Deno:
Inspired by: deno-image