Skip to main content

High-performance perceptual hashing library (CFFI bindings)

Project description

python-libphash

High-performance Python bindings for libphash, a C library for perceptual image hashing.

License: MIT Python 3.8+

Overview

libphash provides multiple algorithms to generate "perceptual hashes" of images. Unlike cryptographic hashes (like MD5 or SHA256), perceptual hashes change only slightly if the image is resized, compressed, or has minor color adjustments. This makes them ideal for finding duplicate or similar images.

Supported Algorithms

  • 64-bit Hashes (uint64):
    • ahash: Average Hash
    • dhash: Difference Hash
    • phash: Perceptual Hash (DCT based)
    • whash: Wavelet Hash
    • mhash: Median Hash
  • Digest Hashes (Multi-byte):
    • bmh: Block Mean Hash
    • color_hash: Color Moment Hash
    • radial_hash: Radial Variance Hash

Installation

Prerequisites

  • A C compiler (GCC/Clang or MSVC)
  • Python 3.8 or higher

Install from source

git clone --recursive https://github.com/yourusername/python-libphash.git
cd python-libphash
pip install .

Quick Start

Basic Usage

from libphash import ImageContext, HashMethod, hamming_distance

# Use the context manager for automatic memory management
with ImageContext("photo.jpg") as ctx:
    # Get standard 64-bit hashes
    phash_val = ctx.phash
    dhash_val = ctx.dhash
    
    print(f"pHash: {phash_val:016x}")
    print(f"dHash: {dhash_val:016x}")

# Compare two images
from libphash import compare_images
distance = compare_images("image1.jpg", "image2.jpg", method=HashMethod.PHASH)
print(f"Hamming Distance: {distance}")

Working with Digests (Advanced Hashes)

Algorithms like Radial Hash or Color Hash return a Digest object instead of a single integer.

with ImageContext("photo.jpg") as ctx:
    digest = ctx.radial_hash
    print(f"Digest size: {digest.size} bytes")
    print(f"Raw data: {digest.data.hex()}")

# Comparing digests
with ImageContext("photo_v2.jpg") as ctx2:
    digest2 = ctx2.radial_hash
    
    # Hamming distance for bit-wise comparison
    h_dist = digest.distance_hamming(digest2)
    
    # L2 (Euclidean) distance for similarity
    l2_dist = digest.distance_l2(digest2)

API Reference

ImageContext

The main class for loading images and computing hashes.

  • __init__(path=None, bytes_data=None): Load an image from a file path or memory.
  • set_gamma(gamma: float): Set gamma correction (useful for Radial Hash).
  • Properties: ahash, dhash, phash, whash, mhash (returns int).
  • Properties: bmh, color_hash, radial_hash (returns Digest).

Digest

  • data: The raw bytes of the hash.
  • size: Length of the hash in bytes.
  • distance_hamming(other): Calculates bit-wise distance.
  • distance_l2(other): Calculates Euclidean distance.

Utilities

  • hamming_distance(h1: int, h2: int): Returns the number of differing bits between two 64-bit integers.
  • get_hash(path, method): Quick way to get a hash without manual context management.
  • compare_images(path1, path2, method): Returns the Hamming distance between two image files.

Performance

Since the core logic is implemented in C and uses stb_image for decoding, libphash is significantly faster than pure-Python alternatives. It also uses CFFI's "out-of-line" mode for minimal overhead.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_libphash-1.0.0.tar.gz (227.7 kB view details)

Uploaded Source

File details

Details for the file python_libphash-1.0.0.tar.gz.

File metadata

  • Download URL: python_libphash-1.0.0.tar.gz
  • Upload date:
  • Size: 227.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for python_libphash-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c417f08cb449064b9d4e9b52fa83729eaec35002c658e414828394575f5f1b42
MD5 b466fcae02fda4d855e4e718ac48eeb9
BLAKE2b-256 2aa6181460f1c878577d89f3441045c66c598dbfe1bffc4a685d2232b7be6d06

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page