optimization: image size

This commit is contained in:
Jörn-Michael Miehe 2023-11-16 14:55:02 +01:00
parent daa959bd14
commit 0db0e92b13

View file

@ -63,13 +63,23 @@ async def find_images_by_prefix(
response_class=StreamingResponse, response_class=StreamingResponse,
) )
async def get_image_by_prefix( async def get_image_by_prefix(
cfg: Config = Depends(get_config),
remote_path: str = Depends(RP_IMAGE), remote_path: str = Depends(RP_IMAGE),
name: str = Depends(LM_IMAGE.getter.func), name: str = Depends(LM_IMAGE.getter.func),
) -> StreamingResponse: ) -> StreamingResponse:
cfg = await get_config()
img = Image.open(BytesIO(await WebDAV.read_bytes(f"{remote_path}/{name}"))) img = Image.open(BytesIO(await WebDAV.read_bytes(f"{remote_path}/{name}")))
img_buffer = BytesIO() img_buffer = BytesIO()
width, height = img.size
target_height = cfg.image.height
target_width = int(width * target_height / height)
img = img.resize(
size=(target_width, target_height),
resample=Image.LANCZOS,
)
img.save(img_buffer, **cfg.image.save_params) img.save(img_buffer, **cfg.image.save_params)
img_buffer.seek(0) img_buffer.seek(0)