import os from datetime import datetime from PIL import Image, ExifTags # Variables INPUT_DIR = "pictures_inbox" DONE_DIR = "pictures_done" OUTPUT_DIR = "output/pictures" YEAR = datetime.now().strftime("%Y") SMALL_SUFFIX = "_small" IMAGE_FORMAT = "jpg" max_width_resized=600 # pixels max de largeur url_folder_pics="https://www.tykayn.fr/wp-uploads/content/i" # Créer le dossier de sortie s'il n'existe pas os.makedirs(os.path.join(OUTPUT_DIR, YEAR), exist_ok=True) def get_exif_orientation(image): """Obtenir l'orientation de l'image à partir des métadonnées EXIF.""" try: exif = image._getexif() if exif is not None: for orientation in ExifTags.TAGS.keys(): if ExifTags.TAGS[orientation] == 'Orientation': return exif[orientation] except AttributeError: pass return None def apply_orientation(image, orientation): """Appliquer l'orientation à l'image.""" if orientation == 3: return image.rotate(180, expand=True) elif orientation == 6: return image.rotate(270, expand=True) elif orientation == 8: return image.rotate(90, expand=True) return image if len(os.listdir(INPUT_DIR)): print('traitement des images dans pictures inbox', len(os.listdir(INPUT_DIR))) # Parcourir toutes les images dans le dossier d'entrée for filename in os.listdir(INPUT_DIR): file_path = os.path.join(INPUT_DIR, filename) # Vérifier si c'est bien un fichier if os.path.isfile(file_path) and (file_path.lower().endswith('.jpg') or file_path.lower().endswith('.png')): # Récupérer le nom de base de l'image et son extension base_name = os.path.splitext(filename) extension = os.path.splitext(filename)[1].lower() # Créer le nom pour la version réduite small_image_name = f"{base_name[0]}{SMALL_SUFFIX}.{IMAGE_FORMAT}" # Chemins des images input_image_path = os.path.join(INPUT_DIR, filename) done_image_path = os.path.join(DONE_DIR, filename) small_image_path = os.path.join(OUTPUT_DIR, YEAR, small_image_name) original_image_path = os.path.join(OUTPUT_DIR, YEAR, filename) # Ouvrir l'image et appliquer l'orientation with Image.open(input_image_path) as img: orientation = get_exif_orientation(img) if orientation is not None: img = apply_orientation(img, orientation) # Redimensionner l'image img = img.resize((max_width_resized, int(img.height * max_width_resized / img.width)), Image.Resampling.LANCZOS) img.save(small_image_path, 'JPEG') # Utiliser 'JPEG' au lieu de 'JPG' # Copier l'image originale dans le dossier de sortie with open(input_image_path, 'rb') as f: with open(original_image_path, 'wb') as f_out: f_out.write(f.read()) # déplacer l'image originale dans le dossier des images traitées os.rename(input_image_path, done_image_path) # Écrire la ligne pour le document .org org_line = f"\n\n[[{url_folder_pics}/{YEAR}/{small_image_name}][{filename}]]\n" with open(os.path.join("output", f"images_{YEAR}.org"), "a") as org_file: org_file.write(org_line) print("Traitement terminé.")