Photo/Photo_heure_dossier_dans_dossier.py

220 lines
8.2 KiB
Python

<<<<<<< HEAD
import os
import shutil
from datetime import datetime
import google.generativeai as genai
import PIL.Image
# --------------------- CONFIGURATION ---------------------
photo_folder = r"C:\Users\Antoine\PycharmProjects\PHOTO\photo a organiser"
dossier_folder = r"C:\Users\Antoine\PycharmProjects\PHOTO\dossier"
# REMPLACER PAR VOTRE CLÉ
genai.configure(api_key="AIzaSyDq2LmX_fwKGAwxGAtmBfX940vT2wDQzBU")
# Gemini 1.5 Flash is excellent for OCR/Barcodes
model = genai.GenerativeModel('models/gemini-2.5-flash')
if not os.path.exists(dossier_folder):
os.makedirs(dossier_folder)
# --------------------- FONCTIONS ---------------------
def get_time_from_filename(filename):
try:
# Supposant un format : QuelqueChose_123045.jpg (HHMMSS)
base = os.path.splitext(filename)[0]
parts = base.split("_")
time_part = parts[-1] # On prend la dernière partie avant l'extension
return datetime.strptime(time_part, "%H%M%S")
except Exception:
return None
def read_barcode(photo_path):
"""
Détecte le contenu d'un code-barre.
Retourne la valeur ou 'inconnu' s'il n'y en a pas.
"""
try:
with PIL.Image.open(photo_path) as img:
response = model.generate_content([
"Retourne uniquement le texte ou les chiffres du code-barre présent sur l'image. "
"Si aucun code-barre n'est visible, répond 'inconnu'.",
img
])
res = response.text.strip().lower()
if "inconnu" in res or len(res) > 50: # Sécurité si le modèle divague
return "inconnu"
return response.text.strip().replace(" ", "")
except Exception as e:
print(f"Erreur lors de la lecture de {photo_path} : {e}")
return "inconnu"
# --------------------- TRAITEMENT ---------------------
photos_raw = [f for f in os.listdir(photo_folder) if f.lower().endswith((".jpg", ".jpeg", ".png"))]
photo_times = []
for photo in photos_raw:
time_obj = get_time_from_filename(photo)
if time_obj:
photo_times.append((photo, time_obj))
# Trier par heure
photo_times.sort(key=lambda x: x[1])
groups = []
for photo, time_obj in photo_times:
if not groups:
groups.append({"photos": [photo], "reference_time": time_obj})
else:
current_group = groups[-1]
# On compare avec la dernière photo du groupe actuel
difference = abs((time_obj - current_group["reference_time"]).total_seconds())
if difference <= 60:
current_group["photos"].append(photo)
current_group["reference_time"] = time_obj # Update reference to the latest photo
else:
groups.append({"photos": [photo], "reference_time": time_obj})
# --------------------- DÉPLACEMENT ET SOUS-DOSSIERS ---------------------
for i, group in enumerate(groups, start=1):
# Dossier principal pour le groupe (ex: Dossier_1)
group_folder_name = f"{i}"
group_folder_path = os.path.join(dossier_folder, group_folder_name)
os.makedirs(group_folder_path, exist_ok=True)
print(f"Traitement du groupe {i} ({len(group['photos'])} photos)...")
for photo_name in group["photos"]:
src_path = os.path.join(photo_folder, photo_name)
# Vérifier si CETTE photo est un code-barre
barcode_value = read_barcode(src_path)
if barcode_value != "inconnu":
# Nettoyage du nom pour Windows
barcode_clean = "".join(c for c in barcode_value if c.isalnum() or c in "-_").strip()
# Création du SOUS-DOSSIER dans le dossier du groupe
subfolder_path = os.path.join(group_folder_path, barcode_clean)
os.makedirs(subfolder_path, exist_ok=True)
dst_path = os.path.join(subfolder_path, photo_name)
print(f" -> Code-barre détecté ({barcode_clean}), déplacé dans sous-dossier.")
else:
# Photo normale, reste à la racine du dossier groupe
dst_path = os.path.join(group_folder_path, photo_name)
shutil.move(src_path, dst_path)
=======
import os
import shutil
from datetime import datetime
import google.generativeai as genai
import PIL.Image
# --------------------- CONFIGURATION ---------------------
photo_folder = r"C:\Users\Antoine\PycharmProjects\PHOTO\photo a organiser"
dossier_folder = r"C:\Users\Antoine\PycharmProjects\PHOTO\dossier"
# REMPLACER PAR VOTRE CLÉ
genai.configure(api_key="AIzaSyDq2LmX_fwKGAwxGAtmBfX940vT2wDQzBU")
# Gemini 1.5 Flash is excellent for OCR/Barcodes
model = genai.GenerativeModel('models/gemini-2.5-flash')
if not os.path.exists(dossier_folder):
os.makedirs(dossier_folder)
# --------------------- FONCTIONS ---------------------
def get_time_from_filename(filename):
try:
# Supposant un format : QuelqueChose_123045.jpg (HHMMSS)
base = os.path.splitext(filename)[0]
parts = base.split("_")
time_part = parts[-1] # On prend la dernière partie avant l'extension
return datetime.strptime(time_part, "%H%M%S")
except Exception:
return None
def read_barcode(photo_path):
"""
Détecte le contenu d'un code-barre.
Retourne la valeur ou 'inconnu' s'il n'y en a pas.
"""
try:
with PIL.Image.open(photo_path) as img:
response = model.generate_content([
"Retourne uniquement le texte ou les chiffres du code-barre présent sur l'image. "
"Si aucun code-barre n'est visible, répond 'inconnu'.",
img
])
res = response.text.strip().lower()
if "inconnu" in res or len(res) > 50: # Sécurité si le modèle divague
return "inconnu"
return response.text.strip().replace(" ", "")
except Exception as e:
print(f"Erreur lors de la lecture de {photo_path} : {e}")
return "inconnu"
# --------------------- TRAITEMENT ---------------------
photos_raw = [f for f in os.listdir(photo_folder) if f.lower().endswith((".jpg", ".jpeg", ".png"))]
photo_times = []
for photo in photos_raw:
time_obj = get_time_from_filename(photo)
if time_obj:
photo_times.append((photo, time_obj))
# Trier par heure
photo_times.sort(key=lambda x: x[1])
groups = []
for photo, time_obj in photo_times:
if not groups:
groups.append({"photos": [photo], "reference_time": time_obj})
else:
current_group = groups[-1]
# On compare avec la dernière photo du groupe actuel
difference = abs((time_obj - current_group["reference_time"]).total_seconds())
if difference <= 60:
current_group["photos"].append(photo)
current_group["reference_time"] = time_obj # Update reference to the latest photo
else:
groups.append({"photos": [photo], "reference_time": time_obj})
# --------------------- DÉPLACEMENT ET SOUS-DOSSIERS ---------------------
for i, group in enumerate(groups, start=1):
# Dossier principal pour le groupe (ex: Dossier_1)
group_folder_name = f"{i}"
group_folder_path = os.path.join(dossier_folder, group_folder_name)
os.makedirs(group_folder_path, exist_ok=True)
print(f"Traitement du groupe {i} ({len(group['photos'])} photos)...")
for photo_name in group["photos"]:
src_path = os.path.join(photo_folder, photo_name)
# Vérifier si CETTE photo est un code-barre
barcode_value = read_barcode(src_path)
if barcode_value != "inconnu":
# Nettoyage du nom pour Windows
barcode_clean = "".join(c for c in barcode_value if c.isalnum() or c in "-_").strip()
# Création du SOUS-DOSSIER dans le dossier du groupe
subfolder_path = os.path.join(group_folder_path, barcode_clean)
os.makedirs(subfolder_path, exist_ok=True)
dst_path = os.path.join(subfolder_path, photo_name)
print(f" -> Code-barre détecté ({barcode_clean}), déplacé dans sous-dossier.")
else:
# Photo normale, reste à la racine du dossier groupe
dst_path = os.path.join(group_folder_path, photo_name)
shutil.move(src_path, dst_path)
>>>>>>> fd17b0aa2007e272777fcf6570af9c6af1b2b0a3
print("\nOpération terminée avec succès !")