from PIL import Image import imagehash import os def calculate_image_hash(image_path): """Вычисляет перцептивный хеш изображения.""" try: img = Image.open(image_path) hash_val = imagehash.phash(img) return str(hash_val) except Exception as e: print(f"Hashing Error: {e}") return None def compare_hashes(hash1, hash2, threshold=5): """Сравнивает два хеша. Если разница меньше порога, изображения считаются похожими.""" h1 = imagehash.hex_to_hash(hash1) h2 = imagehash.hex_to_hash(hash2) diff = h1 - h2 return diff <= threshold