ImageHash
Library containing perceptual hash algorithms using the ImageSharp library
Install / Use
/learn @coenm/ImageHashREADME
Contents
Perceptual hash
Definition by phash.org
A perceptual hash is a fingerprint of a multimedia file derived from various features from its content. Unlike cryptographic hash functions which rely on the avalanche effect of small changes in input leading to drastic changes in the output, perceptual hashes are "close" to one another if the features are similar.
Hash Algorithms
This project implements the following algorithms:
- AverageHash by Dr. Neal Krawetz. Check his blog/article here;
- PerceptualHash also by Dr. Neal Krawetz.
- DifferenceHash by David Oftedal with minor improvements of Dr. Neal Krawetz. More information can be found here or here.
Credits
- This project uses the ImageSharp library for image processing;
- Also based on existing implementations of already listed algorithms: jenssegers/imagehash and jforshee/ImageHashing;
- Icon created by Rik.
API
Calculate image hash
<!-- snippet: CalculateImageHash --><a id='snippet-CalculateImageHash'></a>
var hashAlgorithm = new AverageHash();
// or one of the other available algorithms:
// var hashAlgorithm = new DifferenceHash();
// var hashAlgorithm = new PerceptualHash();
string filename = "your filename";
using var stream = File.OpenRead(filename);
ulong imageHash = hashAlgorithm.Hash(stream);
<sup><a href='/tests/ImageHash.Test/Examples.cs#L14-L26' title='Snippet source file'>snippet source</a> | <a href='#snippet-CalculateImageHash' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->Calculate image similarity
Note that to calculate the image similarity, both image hashes should have been calculated using the same hash algorihm.
<!-- snippet: CalculateSimilarity --><a id='snippet-CalculateSimilarity'></a>
// calculate the two image hashes
ulong hash1 = hashAlgorithm.Hash(imageStream1);
ulong hash2 = hashAlgorithm.Hash(imageStream2);
double percentageImageSimilarity = CompareHash.Similarity(hash1, hash2);
<sup><a href='/tests/ImageHash.Test/Examples.cs#L35-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-CalculateSimilarity' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->You can also take a look at DotNet APIs.
Some screenshots of the demo application



Related Skills
node-connect
347.6kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.6kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.6kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
