drash is a Rust-based command-line tool and library for calculating perceptual hashes of images and comparing them to detect duplicates. This tool supports three common perceptual hashing algorithms:
- dHash (Difference Hash): This is the default algorithm. It's fast and effective, but less accurate than pHash.
- aHash (Average Hash): This algorithm is even faster than dHash, but also less accurate.
- pHash (Perceptual Hash): This is the most accurate of the three, but also the slowest. It's based on the Discrete Cosine Transform (DCT) and is more robust to image modifications.
To use drash, you need to have Rust installed on your system. You can install Rust from here.
Clone the repository and build the project using Cargo:
cargo build --releaseTo run the project, provide two image file paths as arguments. You can also specify the hashing algorithm to use with the --algorithm flag.
cargo run --release -- --algorithm <algorithm> <path_to_image_1> <path_to_image_2>For example:
cargo run --release -- --algorithm phash images/image1.png images/image2.png5 bit differs out of 64 (7.8%)
Potentially a variationThis crate can also be used as a library. To use it, add the following to your Cargo.toml:
[dependencies]
drash = "0.1.0"You can then use the dhash, ahash, and phash functions in your own code:
use drash::{ahash, dhash, phash, load_resize_grayscale};
use std::path::Path;
fn main() {
let image_path = Path::new("path/to/your/image.png");
let image = load_resize_grayscale(image_path).unwrap();
let pixels = image.iter().as_slice();
let dhash_hash = dhash(pixels, 8);
let ahash_hash = ahash(image_path).unwrap();
let phash_hash = phash(image_path).unwrap();
}This code was inspired by Dr. Neal Krawetz blogpost