Найти дубликаты по perceptual hash
Сравнивает отпечатки изображений и собирает группы похожих кадров, устойчивые к сжатию и мелким правкам.
Что делает скрипт
Скрипт вычисляет 64-битный dHash по яркости, затем объединяет изображения с расстоянием Хэмминга не больше порога. Это находит копии после пересжатия, изменения размера и легкой цветокоррекции. Отчет выводится в консоль или JSON, файлы не удаляются.
Быстрый старт
python find_duplicates_phash.py --dir ./photos --threshold 4
Скачайте файл, откройте терминал в папке со скриптом и выполните команду.
В Linux и macOS интерпретатор может называться python3, в Windows - python или py.
Другие варианты
python find_duplicates_phash.py --dir ./photos --threshold 4python find_duplicates_phash.py --dir ./photos --threshold 8 --json duplicates.json --recursive
Что понадобится
winget install Python.Python.3.12
brew install python
sudo apt install python3
py -m pip install Pillow
python3 -m pip install Pillow
python3 -m pip install Pillow
find_duplicates_phash.py
#!/usr/bin/env python3
"""Поиск визуальных дубликатов по dHash."""
from __future__ import annotations
import argparse
import json
import sys
from collections import defaultdict
from pathlib import Path
try:
from PIL import Image, ImageOps
except ImportError:
sys.exit("Нужен Pillow: python3 -m pip install Pillow")
EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp", ".bmp", ".tif", ".tiff"}
def collect(root: Path, recursive: bool) -> list[Path]:
pattern = "**/*" if recursive else "*"
return sorted(
path for path in root.glob(pattern)
if path.is_file() and path.suffix.lower() in EXTENSIONS
)
def dhash(path: Path, size: int = 8) -> int:
with Image.open(path) as opened:
image = ImageOps.exif_transpose(opened).convert("L")
image = image.resize((size + 1, size), Image.Resampling.LANCZOS)
pixels = image.tobytes()
value = 0
for row in range(size):
for column in range(size):
left = pixels[row * (size + 1) + column]
right = pixels[row * (size + 1) + column + 1]
value = (value << 1) | (1 if left > right else 0)
return value
def hamming(left: int, right: int) -> int:
return (left ^ right).bit_count()
def build_groups(items: list[tuple[Path, int]], threshold: int) -> list[list[tuple[Path, int]]]:
parent = list(range(len(items)))
def find(index: int) -> int:
while parent[index] != index:
parent[index] = parent[parent[index]]
index = parent[index]
return index
def union(left: int, right: int) -> None:
root_left = find(left)
root_right = find(right)
if root_left != root_right:
parent[root_right] = root_left
for left in range(len(items)):
for right in range(left + 1, len(items)):
if hamming(items[left][1], items[right][1]) <= threshold:
union(left, right)
grouped: dict[int, list[tuple[Path, int]]] = defaultdict(list)
for index, item in enumerate(items):
grouped[find(index)].append(item)
return [group for group in grouped.values() if len(group) > 1]
def main() -> int:
parser = argparse.ArgumentParser(description="Найти дубликаты по perceptual hash")
parser.add_argument("--dir", type=Path, required=True)
parser.add_argument("--threshold", type=int, default=4, help="Максимальное расстояние Хэмминга")
parser.add_argument("--recursive", action="store_true")
parser.add_argument("--json", type=Path)
args = parser.parse_args()
if not 0 <= args.threshold <= 64:
parser.error("--threshold должен быть от 0 до 64")
root = args.dir.expanduser().resolve()
if not root.is_dir():
parser.error(f"Папка не найдена: {root}")
items: list[tuple[Path, int]] = []
for path in collect(root, args.recursive):
try:
items.append((path, dhash(path)))
except Exception as error:
print(f"Пропуск {path}: {error}")
if len(items) < 2:
print("Недостаточно изображений для сравнения.")
return 0
groups = build_groups(items, args.threshold)
if not groups:
print("Похожие изображения не найдены.")
for number, group in enumerate(groups, 1):
print(f"Группа {number}:")
for path, digest in group:
print(f" {path} {digest:016x}")
if args.json:
payload = [
[{"file": str(path), "hash": f"{digest:016x}"} for path, digest in group]
for group in groups
]
report_path = args.json.expanduser().resolve()
report_path.parent.mkdir(parents=True, exist_ok=True)
report_path.write_text(
json.dumps({"threshold": args.threshold, "groups": payload}, ensure_ascii=False, indent=2) + "\n",
encoding="utf-8",
)
print(f"Отчет сохранен: {args.json}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
Скрипты работают с файлами и настройками системы. Перед запуском прочитайте код и проверьте пути. Особенно если скрипт удаляет, перезаписывает или перемещает данные.