Skip to main content

Crate imghash

Crate imghash 

Source
Expand description

§imghash

imghash provides image hashing algorithms for Rust, compatible with the Python imagehash package.

The following hash algorithms are supported:

  • Average hash — compares each pixel to the mean intensity
  • Median hash — compares each pixel to the median intensity
  • Difference hash — compares adjacent pixels in each row
  • Perceptual hash — uses DCT to capture frequency information

§Quick start

use std::path::Path;
use imghash::average_hash;

let path = Path::new("path/to/image.png");
let hash = average_hash(path).unwrap();

// Encode as a hex string
let hex = hash.encode().unwrap();

// Decode back from hex
let decoded = imghash::ImageHash::decode(&hex, 8, 8).unwrap();

// Compare two hashes
let distance = hash.distance(&decoded).unwrap();
assert_eq!(distance, 0);

§Custom hashers

For more control over hash dimensions and color space, use the hasher structs directly:

use std::path::Path;
use imghash::{average::AverageHasher, ColorSpace, ImageHasher};

let hasher = AverageHasher::new(16, 16, ColorSpace::REC601).unwrap();
let hash = hasher.hash_from_path(Path::new("path/to/image.png")).unwrap();

Modules§

average
difference
median
perceptual

Structs§

ImageHash

Enums§

ColorSpace
ImageHashError

Traits§

ImageHasher
Trait for generating image hashes

Functions§

average_hash
Calculate the average hash for an image at the specified path. Uses the default width and height of 8 x 8 pixels. If you want to use something else please directly use the AverageHasher struct.
difference_hash
Calculate the difference hash for an image at the specified path. Uses the default width and height of 8 x 8 pixels. If you want to use something else please directly use the DifferenceHasher struct.
median_hash
Calculate the median hash for an image at the specified path. Uses the default width and height of 8 x 8 pixels. If you want to use something else please directly use the MedianHasher struct.
perceptual_hash
Calculate the perceptual hash for an image at the specified path. Uses the default width and height of 8 x 8 pixels as well as the default factor of 4. If you want to use something else please directly use the PerceptualHasher struct.