A pure Rust implementation and Python bindings of the PhotoDNA perceptual hashing algorithm for image similarity detection.
PhotoDNA provides robust image hashing and comparison capabilities with:
- Pure Rust implementation for maximum performance and safety
- Python bindings via PyO3 for easy integration
- Perceptual hashing that detects similar images even after transformations
- Multiple distance metrics for flexible similarity comparison
Rust Crate: photo-dna
The core implementation in pure Rust.
cargo add photo-dnaPython Package: photo-dna-rs
Python bindings using PyO3.
pip install photo-dna-rsfrom photo_dna_rs import Hash
# Create hashes from images
hash1 = Hash.from_image_path("image1.jpg")
hash2 = Hash.from_image_path("image2.jpg")
# Calculate similarity (0.0 = different, 1.0 = identical)
similarity = hash1.similarity_log2p(hash2)
print(f"Images are {similarity*100:.1f}% similar")
# Convert between formats
hex_string = hash1.to_hex_str()
hash_from_hex = Hash.from_hex_str(hex_string)use photo_dna::Hash;
// Create hashes from image files
let hash1 = Hash::from_image_path("tests/image_1.jpg").unwrap();
let hash2 = Hash::from_image_path("tests/image_2.jpg").unwrap();
// Calculate similarity using log2p metric (recommended)
let similarity = hash1.similarity_log2p(&hash2);
println!("Image similarity: {:.2}%", similarity * 100.0);
// Serialize to hex string
let hex_string = hash1.to_hex_string();
let restored_hash = Hash::from_hex_str(&hex_string).unwrap();# Build both crates
cargo build --all
# Build Python package
cd python
maturin develop# Test Rust crate
cargo test --all-features
# Test Python package
cd python
pytest tests/ -vContributions are welcome! Please follow these guidelines:
- Fork the repository and create a new branch
- Write tests for new functionality
- Update documentation for any changes
- Run tests:
cargo testandpytest tests/ -v - Format code: Use
rustfmtfor Rust code
This implementation is based on the excellent work by ArcaneNibble in the Open Alleged PhotoDNA project. The original reference implementation provided the foundation for this Rust port.
- GitHub Issues: Report bugs and request features
- Pull Requests: Contribute improvements
This project is licensed under GPLv3. See the LICENSE file for details.
Built with ❤️ by the AIL Project for robust image analysis.