Skip to content

pixano.app.routers.thumbnail

get_thumbnail(b64_image_path, settings, max_size=128) async

Generates a thumbnail for image found in media dir.

Parameters:

Name Type Description Default
b64_image_path str

image identifier in the media directory

required
settings Annotated[Settings, Depends(get_settings)]

App settings.

required
max_size PositiveInt

maximal resolution of the image

128

Returns:

Type Description
StreamingResponse

StreamingResponse

Source code in pixano/app/routers/thumbnail.py
@router.get("/{b64_image_path}", name="get_thumbnail")
async def get_thumbnail(
    b64_image_path: str,
    settings: Annotated[Settings, Depends(get_settings)],
    max_size: PositiveInt = 128,
) -> StreamingResponse:
    """Generates a thumbnail for image found in media dir.

    Args:
        b64_image_path: image identifier in the media directory
        settings: App settings.
        max_size: maximal resolution of the image

    Returns:
        StreamingResponse
    """
    try:
        image_path = base64.b64decode(b64_image_path).decode("utf-8")
    except Exception:
        raise HTTPException(status_code=400, detail="Unable to decode the image path.")

    try:
        image = Image.open(settings.media_dir / Path(image_path))  # ou depuis la DB, autre source…
    except Exception:
        raise HTTPException(status_code=404, detail="Requested image cannot be found.")

    media_type = image.get_format_mimetype()
    image_format = image.format
    if max_size > max(image.width, image.height):
        max_size = max(image.width, image.height)
    image.thumbnail((max_size, max_size))  # génère le thumbnail

    buf = BytesIO()
    image.save(buf, format=image_format)
    buf.seek(0)
    return StreamingResponse(buf, media_type=media_type)