imghash 1.1.0

Image hashing for Rust
Documentation

imghash - Image Hashing for Rust

Crates.io Version

imghash is a crate that allows you to generate different hashes for images. The following hashes can be generated using this crate:

Usage

There are multiple ways how to utilize imghash depending on your use case.

Quickstart

The easy way to use imghash is by using the provided utility functions which assume reasonable defaults.

use imghash::{average_hash, difference_hash, perceptual_hash};

let path = Path::new("path/to/my/image");

let average = average_hash(path);
let difference = difference_hash(path);
let perceptual = perceptual_hash(path);

Each of these functions return a Result<ImageHash, String>-type. The ImageHash object is essentially a container for the encoded bit matrix of the image (learn more here). The ImageHash can be encoded into hexadecimal string by calling the encode method:

let res: String = hash.encode();

Custom Hashers

If you need more flexibility, for example getting a larger bit matrix, you can use a custom Hasher instance.

For each hash type the crate provides a custom hasher, here for the example we will use the AverageHasher:

use imghash::{average::AverageHasher};

let path = Path::new("path/to/my/image");

let hasher = AverageHasher {
  width: 10,
  height: 10,
};

let hash = hasher.hash_from_path(path);

Hasher instances also allow you to create hashes for already loaded images:

let img = ImageReader::open(...);

let hasher = AverageHasher { ..Default::default() };

let hash = hasher.hash_from_img(&img);

Each hasher also implements the Default-trait, allowing you to create them with their default values:

let hasher = AverageHasher { ..Default::default() };

Python Compatability

One of the major factors that drove development of this crate was the need to have a hasher implementation that can decode hashes generated by the imagehash-package for Python.

A future Python-wrapper for this create is already on the roadmap, to allow full replacing of the imagehash-package.

However it is important to note that the hashes are not guaranteed to be the same between this crate and the imagehash-package. The encoding / decoding algorithms are 100% compatible, however the underlying image package (Pillow and image) might produce different image bytes after grayscaling.