mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-12-27 06:33:08 +00:00
Jörn-Michael Miehe
af00dafb6c
apparently, a @staticmethod that Depends on another @staticmethod in the same class is bad
46 lines
1 KiB
Python
46 lines
1 KiB
Python
import re
|
|
from io import BytesIO
|
|
|
|
from fastapi.responses import StreamingResponse
|
|
from PIL import Image
|
|
|
|
from .webdav import WebDAV
|
|
|
|
|
|
async def list_images_auto() -> list[str]:
|
|
"""
|
|
Finde alle Bilddateien im "automatisch"-Verzeichnis
|
|
"""
|
|
|
|
return await WebDAV.list_files(
|
|
directory="/images_auto",
|
|
regex=re.compile(r"\.(gif|jpe?g|tiff?|png|bmp)$", flags=re.IGNORECASE),
|
|
)
|
|
|
|
|
|
async def load_image(file_name: str) -> Image.Image:
|
|
"""
|
|
Versuche, Bild aus Datei zu laden
|
|
"""
|
|
|
|
if not await WebDAV.file_exists(file_name):
|
|
raise RuntimeError(f"DAV-File {file_name} does not exist!")
|
|
|
|
return Image.open(BytesIO(await WebDAV.read_bytes(file_name)))
|
|
|
|
|
|
async def api_return_image(img: Image.Image) -> StreamingResponse:
|
|
"""
|
|
Bild mit API zurückgeben
|
|
"""
|
|
|
|
# JPEG-Daten in Puffer speichern
|
|
img_buffer = BytesIO()
|
|
img.save(img_buffer, format="JPEG", quality=85)
|
|
img_buffer.seek(0)
|
|
|
|
# zurückgeben
|
|
return StreamingResponse(
|
|
media_type="image/jpeg",
|
|
content=img_buffer,
|
|
)
|