mirror of
https://github.com/Ladebeze66/llm_ticket3.git
synced 2025-12-16 03:57:47 +01:00
45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
import os
|
|
from PIL import Image
|
|
import imagehash
|
|
from typing import List, Dict
|
|
|
|
def filtrer_images_uniques(image_paths: List[str], seuil_hamming: int = 5) -> List[str]:
|
|
"""
|
|
Supprime les doublons perceptuels parmi une liste d'images.
|
|
|
|
Args:
|
|
image_paths: Liste des chemins vers les images.
|
|
seuil_hamming: Distance de Hamming maximale pour considérer deux images comme identiques.
|
|
|
|
Returns:
|
|
Liste de chemins d'images considérées comme uniques.
|
|
(Les images avec noms/format les plus courts sont conservées)
|
|
"""
|
|
images_uniques = []
|
|
hashes: Dict[str, str] = {}
|
|
|
|
# Trier les chemins pour privilégier les noms courts et formats préférés
|
|
def cle_pertinence(p: str) -> tuple:
|
|
ext = os.path.splitext(p)[1].lower()
|
|
poids_format = {'.png': 0, '.jpeg': 1, '.jpg': 2, '.bmp': 3, '.gif': 4}
|
|
return (poids_format.get(ext, 99), len(os.path.basename(p)))
|
|
|
|
for path in sorted(image_paths, key=cle_pertinence):
|
|
try:
|
|
with Image.open(path) as img:
|
|
img_hash = str(imagehash.phash(img))
|
|
|
|
doublon = False
|
|
for existing_hash in hashes.values():
|
|
if imagehash.hex_to_hash(existing_hash) - imagehash.hex_to_hash(img_hash) <= seuil_hamming:
|
|
doublon = True
|
|
break
|
|
|
|
if not doublon:
|
|
hashes[path] = img_hash
|
|
images_uniques.append(path)
|
|
except Exception as e:
|
|
print(f"[Erreur] Image ignorée ({path}) : {e}")
|
|
|
|
return images_uniques
|