Files
missbleue/watermark.py
2025-05-08 12:15:42 +02:00

46 lines
1.7 KiB
Python

from PIL import Image, ImageOps
import os
from pathlib import Path# main.py
import logging
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")
# 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)
# Save the result
output_path = image_path.rsplit('.', 1)
output_path = f"{output_path[0]}-filigrane.jpg"
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}")
print(f"outpath = {output_path}")
return output_path
#print(f"✅ Image enregistrée : {output_path}")