auto detect tags and move pictures after resizing

This commit is contained in:
Tykayn 2024-11-15 15:58:19 +01:00 committed by tykayn
parent 9c72473913
commit 618c029c62
8 changed files with 165 additions and 154 deletions

View file

@ -1,26 +1,53 @@
# on redimensionne nos images qui ullustreront les articles,
# pour cela on les copie dans notre dossier pictures_inbox.
# elles sont copiées en version réduite, et déplacées dans le dossier de l'année
# ce script génère aussi de quoi copier des liens org affichant les thumbnails de l'image liée à la grande version
import os
from PIL import Image
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(os.path.join(INPUT_DIR, filename)):
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()
@ -30,22 +57,30 @@ for filename in os.listdir(INPUT_DIR):
# 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)
# Redimensionner l'image
# Ouvrir l'image et appliquer l'orientation
with Image.open(input_image_path) as img:
img = img.resize((600, int(img.height * 600 / img.width)), Image.Resampling.LANCZOS)
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"[[file:wp-uploads/content/i/{YEAR}/{small_image_name}][{filename}]]\n"
org_line = f"\n\n[[file:{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é.")
print("Traitement terminé.")