From 1d83d52bc1aacbbfe6dfbcc96780aa3364748469 Mon Sep 17 00:00:00 2001 From: Christian Bastian <80225746+cdb-boop@users.noreply.github.com> Date: Sat, 6 Jul 2024 18:25:11 -0400 Subject: [PATCH] Crop image before Image.thumbnail --- __init__.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/__init__.py b/__init__.py index fc4b28f..feb6ebf 100644 --- a/__init__.py +++ b/__init__.py @@ -322,6 +322,7 @@ async def get_model_preview(request): fp = image_path with Image.open(fp) as image: + format = image.format w0, h0 = image.size if w is None: w = (h * w0) // h0 @@ -337,10 +338,24 @@ async def get_model_preview(request): value_str = str(PIL_cast_serializable(value)) # not sure if this is correct (sometimes includes exif) metadata.add_text(key, value_str) + ratio_original = w0 / h0 + ratio_thumbnail = w / h + if abs(ratio_original - ratio_thumbnail) < 0.01: + crop_box = (0, 0, w0, h0) + elif ratio_original > ratio_thumbnail: + crop_width_fp = h0 * w / h + x0 = int((w0 - crop_width_fp) / 2) + crop_box = (x0, 0, x0 + int(crop_width_fp), h0) + else: + crop_height_fp = w0 * h / w + y0 = int((h0 - crop_height_fp) / 2) + crop_box = (0, y0, w0, y0 + int(crop_height_fp)) + image = image.crop(crop_box) + image.thumbnail((w, h)) image_bytes = io.BytesIO() - image.save(image_bytes, format=image.format, exif=exif, pnginfo=metadata) + image.save(image_bytes, format=format, exif=exif, pnginfo=metadata) image_data = image_bytes.getvalue() return web.Response(