2025-04-24 17:27:31 +02:00
|
|
|
from PIL import Image, ImageOps
|
|
|
|
|
import os
|
|
|
|
|
from pathlib import Path# main.py
|
|
|
|
|
import logging
|
2025-05-18 21:19:20 +02:00
|
|
|
import tempfile
|
2025-04-24 17:27:31 +02:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
logger.info("Logger from watermark")
|
|
|
|
|
|
|
|
|
|
def create_watermark_image(image_path, filigrane_path="logo-lescreationsdemissbleue-fond-transparent.png"):
|
|
|
|
|
#image = Image.open(image_path).convert("RGBA")
|
|
|
|
|
image = ImageOps.exif_transpose(Image.open(image_path)).convert("RGBA")
|
|
|
|
|
filigrane = Image.open(filigrane_path).convert("RGBA")
|
2025-05-18 21:19:20 +02:00
|
|
|
temp_dir = tempfile.gettempdir()
|
2025-04-24 17:27:31 +02:00
|
|
|
|
|
|
|
|
# Resize the watermak (ex: 25% of widht from principal image)
|
|
|
|
|
"""ratio = 0.25
|
|
|
|
|
"""
|
|
|
|
|
ratio = 0.15 # instead of 0.25
|
|
|
|
|
new_width = int(image.width * ratio)
|
|
|
|
|
new_height = int(filigrane.height * (new_width / filigrane.width))
|
|
|
|
|
filigrane = filigrane.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
|
|
|
|
|
|
|
|
|
# Change the color in grey and handle the transparency
|
|
|
|
|
"""filigrane = filigrane.convert("L").convert("RGBA") # Gris
|
|
|
|
|
alpha = filigrane.getchannel("A").point(lambda p: int(p * 0.3)) # ~30% opacité
|
|
|
|
|
filigrane.putalpha(alpha)"""
|
|
|
|
|
|
|
|
|
|
x = image.width - filigrane.width - 30 # 10px from right edge
|
|
|
|
|
y = image.height - filigrane.height - 30 # 10px from bottom edge
|
|
|
|
|
|
|
|
|
|
# Paste watermark (with alpha mask)
|
|
|
|
|
image.paste(filigrane, (x, y), filigrane)
|
|
|
|
|
|
2025-05-18 21:19:20 +02:00
|
|
|
temp_image_path = os.path.join(temp_dir, image_path)
|
|
|
|
|
|
2025-04-24 17:27:31 +02:00
|
|
|
# Save the result
|
2025-05-18 21:19:20 +02:00
|
|
|
#output_path = image_path.rsplit('.', 1)
|
|
|
|
|
#output_path = f"{output_path[0]}.jpg"
|
|
|
|
|
output_path = os.path.basename(temp_image_path)
|
2025-04-24 17:27:31 +02:00
|
|
|
try:
|
|
|
|
|
if not os.path.exists(output_path):
|
|
|
|
|
image.convert("RGB").save(output_path, "JPEG")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.exception(f"🔥 Image avec filigrane existe déjà : {e} - {Path(output_path).name}")
|
|
|
|
|
|
2025-05-18 21:19:20 +02:00
|
|
|
#print(f"outpath = {output_path}")
|
2025-04-24 17:27:31 +02:00
|
|
|
return output_path
|
|
|
|
|
#print(f"✅ Image enregistrée : {output_path}")
|
|
|
|
|
|