ImageHash is a Zig package for generating robust image fingerprints using four popular algorithms: average (ahash), difference (dhash), perceptual (phash), and wavelet (whash) hashing.
-
Multiple Hash Algorithms:
- ahash (Average Hash)
- dhash (Difference Hash)
- phash (Perceptual Hash)
- whash (Wavelet Hash)
You can use the built‑in Zig fetcher to download and pin a tarball:
zig fetch --save git+https://github.com/galactixx/imagehash#v0.2.0This adds an
imagehashentry under.dependenciesin yourbuild.zig.zon.
Then in your build.zig:
const imagehash_mod = b.dependency("imagehash", .{
.target = target,
.optimize = optimize,
}).module("imagehash");
// add to library
lib_mod.addImport("imagehash", imagehash_mod);
// add to executable
exe.root_module.addImport("imagehash", imagehash_mod);This lets you const ih = @import("imagehash"); in your Zig code.
const std = @import("std");
const ih = @import("imagehash");
pub fn main() !void {
const file = "testdata/checkerboard.png";
// Compute all four hashes
const ahash = try ih.averageHash(file);
const dhash = try ih.differenceHash(file);
const phash = try ih.perceptualHash(file);
const whash = try ih.waveletHash(file);
// Print hex digests
var buf: [16]u8 = undefined;
std.debug.print("ahash: {s}\n", .{ahash.hexDigest(buf[0..])});
std.debug.print("phash: {s}\n", .{phash.hexDigest(buf[0..])});
// Compare two hashes
const dist = ahash.distance(dhash);
std.debug.print("Hamming distance: {}\n", .{dist});
}Computes an 8×8 average-based hash (ahash).
Computes a 9×8 horizontal difference-based hash (dhash).
s
Computes a 32×32 perceptual (DCT-based) hash and reduces to an 8×8 block (phash).
Computes a 64×64 wavelet-based hash with 4-level decomposition (whash).
Returns the Hamming distance between two hashes.
Serialize an ImageHash to a JSON byte slice.
Parse an ImageHash from JSON.
This project is licensed under the MIT License. See the LICENSE file for details.
Have questions or need help? Open an issue on the GitHub repository.
