abonander / img_hash

A Rust library for calculating perceptual hash values of images
Apache License 2.0
310 stars 62 forks source link

Produced gradient hash has an invalid size #25

Closed ksesong closed 4 years ago

ksesong commented 5 years ago

I've been testing this library with this simple script, but I can't figure out why the produced hash has an invalid size (here 56 instead of 64 bits). The size is also incorrect for DoubleGradient with 135 instead of 128 bits. When using the Mean, Block or DCT hashtype, the resulting hash has the correct length (64 bits). (I'm a novice in Rust, so I might have done something wrong.)

extern crate image;
extern crate img_hash;

use std::path::Path;
use img_hash::{ImageHash, HashType};

fn main() {
    let img = image::open(&Path::new("00002701.jpg")).unwrap();
    let hash = ImageHash::hash(&img, 8, HashType::Gradient);
    println!("{:?}", hash.bitv);
    println!("{}", hash.size());
}

(The image is just a random picture, I am only looking at the hash size here.)

abonander commented 5 years ago

I'm actually working on an API/implementation overhaul on the 3-alpha branch. Can you try that and let me know if it's still the same issue or if it's fixed? If the latter, I'll consider this closed when I go to merge that.

ksesong commented 5 years ago

So I've been trying the 3-alpha branch version, but I can't manage to get a non-empty hash:

extern crate image;
extern crate img_hash;

use img_hash::{HasherConfig, HashAlg};
use image::GenericImageView;

fn main() {
    let img = image::open("00002701.jpg").unwrap();
    println!("{:?}", img.dimensions()); // Returns (643, 488)
    let hasher_config = HasherConfig::new().hash_alg(HashAlg::Mean).hash_size(8, 8);
    println!("{:?}", hasher_config); // Returns HasherConfig { width: 8, height: 8, hash_alg: Mean, resize_filter: "Lanczos3", gauss_sigmas: None, use_dct: false }
    let hasher = hasher_config.to_hasher();
    let hash = hasher.hash_image(&img);
    println!("{:?}", hash.to_base64()); // Returns ""
}

Do you see anything wrong in my code?

abonander commented 5 years ago

Sorry, that was a bug. I added a regression test. Run cargo update and try again.

ksesong commented 5 years ago

I did some more testing with the 3-alpha branch and it's fixed. Thanks for the library!

abonander commented 4 years ago

Closed with 3.0.