ImageHash

4.3.2 · active · verified Thu Apr 09

ImageHash is a Python library that provides tools for generating perceptual hash values for images. These hashes can be used to compare images based on their visual content, making it useful for finding similar or duplicate images. It supports various hashing algorithms like aHash, pHash, dHash, wHash, colorhash, and crop-resistant hashing. The current version is 4.3.2, and it receives updates periodically to add features, improve performance, and address bugs. [1, 3, 8]

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load an image using Pillow, generate an average perceptual hash using `imagehash.average_hash`, and compare it to another hash by calculating their Hamming distance. A smaller difference typically indicates greater visual similarity. [1, 3, 8, 14]

from PIL import Image
import imagehash
import os

# Create a dummy image for demonstration if not available
dummy_image_path = 'dummy_image.png'
if not os.path.exists(dummy_image_path):
    try:
        from PIL import ImageDraw
        img = Image.new('RGB', (200, 200), color = 'red')
        d = ImageDraw.Draw(img)
        d.text((10,10), "Hello", fill=(0,0,0))
        img.save(dummy_image_path)
        print(f"Created dummy image: {dummy_image_path}")
    except ImportError:
        print("Pillow is needed to create a dummy image. Please install it.")
        exit()

try:
    # Load an image
    image = Image.open(dummy_image_path)

    # Generate a perceptual hash (e.g., average hash)
    hash_value = imagehash.average_hash(image)
    print(f"Hash for '{dummy_image_path}': {hash_value}")

    # You can also generate other types of hashes:
    # phash_value = imagehash.phash(image)
    # dhash_value = imagehash.dhash(image)
    # whash_value = imagehash.whash(image)
    # colorhash_value = imagehash.colorhash(image)

    # To compare with another image:
    # For demonstration, let's pretend to load a slightly different image
    # In a real scenario, this would be another actual image file
    slightly_different_image = Image.new('RGB', (200, 200), color = 'red')
    d = ImageDraw.Draw(slightly_different_image)
    d.text((15,15), "Hello", fill=(0,0,0)) # slight shift
    
    other_hash_value = imagehash.average_hash(slightly_different_image)
    print(f"Hash for a slightly different image: {other_hash_value}")

    # Calculate the Hamming distance (difference) between hashes
    difference = hash_value - other_hash_value
    print(f"Difference between hashes: {difference}")

    # A smaller difference indicates greater similarity
    if difference < 5:
        print("The images are considered similar (difference < 5).")
    else:
        print("The images are considered different (difference >= 5).")

except FileNotFoundError:
    print(f"Error: Image file not found at {dummy_image_path}")
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →