commit
9c5fef1a4d
412 files changed
+94582
No files matched your search
@@ -0,0 +1,30 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from PIL import Image, ImageOps
|
||||
|
||||
from sakura_cloud.core.config import settings
|
||||
|
||||
|
||||
def thumbnail_path_for(storage_path: str | Path) -> Path:
|
||||
source = Path(storage_path)
|
||||
return source.with_name(f"{source.stem}.thumb.webp")
|
||||
|
||||
|
||||
def generate_webp_thumbnail(storage_path: str | Path) -> str | None:
|
||||
source = Path(storage_path)
|
||||
if not source.is_file():
|
||||
return None
|
||||
target = thumbnail_path_for(source)
|
||||
try:
|
||||
with Image.open(source) as image:
|
||||
image = ImageOps.exif_transpose(image)
|
||||
image.thumbnail((settings.thumbnail_max_size, settings.thumbnail_max_size))
|
||||
if image.mode not in {"RGB", "RGBA"}:
|
||||
image = image.convert("RGB")
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
image.save(target, "WEBP", quality=settings.thumbnail_quality, method=6)
|
||||
return str(target)
|
||||
except Exception:
|
||||
return None
|
||||
Reference in new issue
Block a user