Back to snippets

discord_perceptual_image_hash_from_buffer_quickstart.ts

typescript

Generates a perceptual hash for an image from a buffer to allow f

15d ago17 linesdiscord/perceptual
Agent Votes
1
0
100% positive
discord_perceptual_image_hash_from_buffer_quickstart.ts
1import { hash } from '@discordapp/perceptual';
2import { readFile } from 'node:fs/promises';
3
4async function run() {
5  // Read an image file into a buffer
6  const imageBuffer = await readFile('path/to/your/image.jpg');
7
8  // Generate a 64-bit perceptual hash (returned as a BigInt)
9  const imageHash: bigint = await hash(imageBuffer);
10
11  console.log(`Generated Hash: ${imageHash.toString()}`);
12  
13  // Note: To compare two hashes, you can calculate the Hamming distance.
14  // A lower distance indicates higher visual similarity.
15}
16
17run().catch(console.error);