Files
fund-tracer/backend/app/services/image_service.py

25 lines
672 B
Python
Raw Normal View History

2026-03-11 16:28:04 +08:00
"""Image post-processing helpers (thumbnail generation, etc.)."""
from pathlib import Path
from PIL import Image
from app.core.config import settings
def generate_thumbnail(file_path: str, max_size: int = 400) -> str:
full = settings.upload_path / file_path
if not full.exists():
return file_path
thumb_dir = full.parent / "thumbs"
thumb_dir.mkdir(exist_ok=True)
thumb_path = thumb_dir / full.name
try:
with Image.open(full) as img:
img.thumbnail((max_size, max_size))
img.save(thumb_path)
return str(thumb_path.relative_to(settings.upload_path))
except Exception:
return file_path