82 lines
2.6 KiB
Python
Executable File
82 lines
2.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import sys
|
|
import os
|
|
import subprocess
|
|
from PIL import Image
|
|
|
|
def add_logo(image_path, logo_svg_path):
|
|
if not os.path.exists(image_path):
|
|
print(f"Error: Image not found at {image_path}")
|
|
return
|
|
|
|
if not os.path.exists(logo_svg_path):
|
|
print(f"Error: Logo SVG not found at {logo_svg_path}")
|
|
return
|
|
|
|
try:
|
|
# Open the main image
|
|
img = Image.open(image_path)
|
|
img_w, img_h = img.size
|
|
|
|
# Calculate desired logo width (e.g., 20% of image width)
|
|
target_logo_width = int(img_w * 0.2)
|
|
if target_logo_width < 100: target_logo_width = 100 # Minimum width
|
|
|
|
# Convert SVG to PNG with the target width using rsvg-convert
|
|
# We use a temporary file for the logo png
|
|
logo_png_path = "/tmp/temp_logo.png"
|
|
|
|
# Check if rsvg-convert exists
|
|
rsvg_check = subprocess.run(["which", "rsvg-convert"], capture_output=True, text=True)
|
|
if rsvg_check.returncode != 0:
|
|
print("Error: rsvg-convert not found. Please install librsvg.")
|
|
return
|
|
|
|
# Convert SVG to PNG
|
|
subprocess.run([
|
|
"rsvg-convert",
|
|
"-w", str(target_logo_width),
|
|
"-f", "png",
|
|
"-o", logo_png_path,
|
|
logo_svg_path
|
|
], check=True)
|
|
|
|
# Open the logo image
|
|
logo = Image.open(logo_png_path).convert("RGBA")
|
|
|
|
# Calculate position (top-left with margin)
|
|
margin = int(img_w * 0.05)
|
|
position = (margin, margin)
|
|
|
|
# Paste logo onto image (using alpha channel as mask)
|
|
# Handle different image modes (RGB vs RGBA)
|
|
if img.mode != 'RGBA':
|
|
img = img.convert('RGBA')
|
|
|
|
img.paste(logo, position, logo)
|
|
|
|
# Save the result (overwrite original or save as new)
|
|
# We'll overwrite for now as per "add the logo" request implies modification
|
|
# But if it's JPG, we need to convert back to RGB
|
|
if image_path.lower().endswith(('.jpg', '.jpeg')):
|
|
img = img.convert('RGB')
|
|
|
|
img.save(image_path)
|
|
print(f"✅ Logo added to {image_path}")
|
|
|
|
# Clean up
|
|
if os.path.exists(logo_png_path):
|
|
os.remove(logo_png_path)
|
|
|
|
except Exception as e:
|
|
print(f"❌ Failed to add logo: {str(e)}")
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 3:
|
|
print("Usage: python3 add_logo.py <image_path> <logo_svg_path>")
|
|
sys.exit(1)
|
|
|
|
image_path = sys.argv[1]
|
|
logo_svg_path = sys.argv[2]
|
|
add_logo(image_path, logo_svg_path)
|