feat: Implement image thumbnail generation, storage, and API endpoints for assets, including a regeneration utility.

This commit is contained in:
xds
2026-02-05 20:52:50 +03:00
parent 736e5a8c12
commit 76dd976854
26 changed files with 127 additions and 20 deletions

Binary file not shown.

27
utils/image_utils.py Normal file
View File

@@ -0,0 +1,27 @@
from io import BytesIO
from typing import Tuple, Optional
from PIL import Image
import logging
logger = logging.getLogger(__name__)
def create_thumbnail(image_data: bytes, size: Tuple[int, int] = (800, 800)) -> Optional[bytes]:
"""
Creates a thumbnail from image bytes.
Returns the thumbnail as bytes (JPEG format) or None if failed.
"""
try:
with Image.open(BytesIO(image_data)) as img:
# Convert to RGB if necessary (e.g. for RGBA/P images saving as JPEG)
if img.mode in ('RGBA', 'P'):
img = img.convert('RGB')
img.thumbnail(size)
thumb_io = BytesIO()
img.save(thumb_io, format='JPEG', quality=85)
thumb_io.seek(0)
return thumb_io.read()
except Exception as e:
logger.error(f"Failed to create thumbnail: {e}")
return None