|
from PIL import Image, ImageDraw, ImageFont |
|
import os |
|
from typing import Dict, Any |
|
|
|
def add_watermark(image_path: str, watermark_text: str) -> Dict[str, Any]: |
|
try: |
|
image = Image.open(image_path) |
|
|
|
overlay = Image.new('RGBA', image.size, (255, 255, 255, 0)) |
|
draw = ImageDraw.Draw(overlay) |
|
|
|
try: |
|
font_size = min(image.width, image.height) // 20 |
|
font = ImageFont.truetype("arial.ttf", font_size) |
|
except: |
|
font = ImageFont.load_default() |
|
|
|
bbox = draw.textbbox((0, 0), watermark_text, font=font) |
|
text_width = bbox[2] - bbox[0] |
|
text_height = bbox[3] - bbox[1] |
|
|
|
x = (image.width - text_width) // 2 |
|
y = (image.height - text_height) // 2 |
|
|
|
text_color = (255, 255, 255, 128) |
|
|
|
draw.text((x-2, y-2), watermark_text, fill=(0, 0, 0, 64), font=font) |
|
draw.text((x, y), watermark_text, fill=text_color, font=font) |
|
|
|
watermarked = Image.alpha_composite(image.convert('RGBA'), overlay) |
|
final_image = watermarked.convert('RGB') |
|
|
|
base_dir = os.path.dirname(image_path) |
|
base_name, ext = os.path.splitext(os.path.basename(image_path)) |
|
new_filename = f"{base_name}_watermarked{ext}" |
|
new_path = os.path.join(base_dir, new_filename) |
|
|
|
final_image.save(new_path, quality=95) |
|
output_size = os.path.getsize(new_path) |
|
|
|
return { |
|
"success": True, |
|
"message": "Watermark added successfully", |
|
"input_path": image_path, |
|
"output_path": new_path, |
|
"output_size_bytes": output_size, |
|
"watermark_text": watermark_text |
|
} |
|
|
|
except Exception as e: |
|
return { |
|
"success": False, |
|
"error": str(e), |
|
"input_path": image_path, |
|
"output_path": None |
|
} |