2024-11-15 01:44:59 +01:00
|
|
|
import os
|
|
|
|
from datetime import datetime
|
|
|
|
|
2024-11-15 15:58:19 +01:00
|
|
|
from PIL import Image, ExifTags
|
|
|
|
|
2024-11-15 01:44:59 +01:00
|
|
|
# Variables
|
|
|
|
INPUT_DIR = "pictures_inbox"
|
2024-11-15 15:58:19 +01:00
|
|
|
DONE_DIR = "pictures_done"
|
2024-11-15 01:44:59 +01:00
|
|
|
OUTPUT_DIR = "output/pictures"
|
|
|
|
YEAR = datetime.now().strftime("%Y")
|
|
|
|
SMALL_SUFFIX = "_small"
|
|
|
|
IMAGE_FORMAT = "jpg"
|
2024-11-15 15:58:19 +01:00
|
|
|
max_width_resized=600 # pixels max de largeur
|
|
|
|
url_folder_pics="https://www.tykayn.fr/wp-uploads/content/i"
|
2024-11-15 01:44:59 +01:00
|
|
|
|
|
|
|
# Créer le dossier de sortie s'il n'existe pas
|
|
|
|
os.makedirs(os.path.join(OUTPUT_DIR, YEAR), exist_ok=True)
|
|
|
|
|
2024-11-15 15:58:19 +01:00
|
|
|
|
|
|
|
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)))
|
2024-11-15 01:44:59 +01:00
|
|
|
# Parcourir toutes les images dans le dossier d'entrée
|
|
|
|
for filename in os.listdir(INPUT_DIR):
|
2024-11-15 15:58:19 +01:00
|
|
|
file_path = os.path.join(INPUT_DIR, filename)
|
2024-11-15 01:44:59 +01:00
|
|
|
# Vérifier si c'est bien un fichier
|
2024-11-15 15:58:19 +01:00
|
|
|
if os.path.isfile(file_path) and (file_path.lower().endswith('.jpg') or file_path.lower().endswith('.png')):
|
2024-11-15 01:44:59 +01:00
|
|
|
# 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)
|
2024-11-15 15:58:19 +01:00
|
|
|
done_image_path = os.path.join(DONE_DIR, filename)
|
2024-11-15 01:44:59 +01:00
|
|
|
small_image_path = os.path.join(OUTPUT_DIR, YEAR, small_image_name)
|
|
|
|
original_image_path = os.path.join(OUTPUT_DIR, YEAR, filename)
|
|
|
|
|
2024-11-15 15:58:19 +01:00
|
|
|
# Ouvrir l'image et appliquer l'orientation
|
2024-11-15 01:44:59 +01:00
|
|
|
with Image.open(input_image_path) as img:
|
2024-11-15 15:58:19 +01:00
|
|
|
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)
|
2024-11-15 01:44:59 +01:00
|
|
|
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())
|
2024-11-15 15:58:19 +01:00
|
|
|
# déplacer l'image originale dans le dossier des images traitées
|
|
|
|
os.rename(input_image_path, done_image_path)
|
2024-11-15 01:44:59 +01:00
|
|
|
# Écrire la ligne pour le document .org
|
2024-11-15 15:58:19 +01:00
|
|
|
|
2024-11-18 13:10:30 +01:00
|
|
|
org_line = f"\n\n[[{url_folder_pics}/{YEAR}/{small_image_name}][{filename}]]\n"
|
2024-11-15 01:44:59 +01:00
|
|
|
with open(os.path.join("output", f"images_{YEAR}.org"), "a") as org_file:
|
|
|
|
org_file.write(org_line)
|
|
|
|
|
2024-11-15 15:58:19 +01:00
|
|
|
print("Traitement terminé.")
|