#perceptual #hash #image #difference #hash-values

bin+lib visual-hash

A simple library that provides perceptual hashing and difference calculation for images

1 stable release

3.3.2 Oct 31, 2022

#9 in #perceptual

Download history 1/week @ 2024-02-19 21/week @ 2024-02-26 5/week @ 2024-03-11 50/week @ 2024-04-01

55 downloads per month
Used in 2 crates (via egui-screenshot-testing)

MIT/Apache

48KB
838 lines

visual-hash

A fork of img_hash

A library for getting perceptual hash values of images.

Thanks to Dr. Neal Krawetz for the outlines of the Mean (aHash), Gradient (dHash), and DCT (pHash) perceptual hash algorithms:
http://www.hackerfactor.com/blog/?/archives/432-Looks-Like-It.html (Accessed August 2014)

Also provides an implementation of the Blockhash.io algorithm.

This crate can operate directly on buffers from the PistonDevelopers/image crate.

Usage

Documentation

Add visual-hash to your Cargo.toml:

[dependencies.visual-hash]
version = "3.0"

Example program:

use clap::Parser;
use visual_hash::HasherConfig;

#[derive(Clone, Debug, Parser)]
struct Args {
    left: String,
    right: String,
}

fn main() {
    let args = Args::parse();

    if let Err(e) = run(&args) {
        eprintln!("{e}");
        std::process::exit(1);
    }
}

fn run(args: &Args) -> anyhow::Result<()> {
    let image1 = image::open(&args.left)?;
    let image2 = image::open(&args.right)?;

    let hasher = HasherConfig::new().to_hasher();

    let hash1 = hasher.hash_image(&image1);
    let hash2 = hasher.hash_image(&image2);

    println!("Image1 hash: {}", hash1.to_base64());
    println!("Image2 hash: {}", hash2.to_base64());

    println!("Hamming Distance: {}", hash1.dist(&hash2));

    Ok(())
}

Benchmarking

In order to build and test on Rust stable, the benchmarks have to be placed behind a feature gate. If you have Rust nightly installed and want to run benchmarks, use the following command:

cargo bench --features bench

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~14MB
~111K SLoC