From 761c4a719b9f0d906a63c3b720b902e87c2a109d Mon Sep 17 00:00:00 2001 From: Dan Bochman Date: Mon, 30 Jun 2025 14:16:01 +0200 Subject: [PATCH] added image processing best practices to encode_img_to_base64 --- fashn_viton.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/fashn_viton.py b/fashn_viton.py index 7f1bf18..db79b84 100644 --- a/fashn_viton.py +++ b/fashn_viton.py @@ -37,6 +37,18 @@ class FASHN: @staticmethod def encode_img_to_base64(img): + """Resizes and encodes an image as a JPEG in Base64 format.""" + # Resize to max 2000px on largest dimension (only downsample, never upscale) + width, height = img.size + if max(width, height) > 2000: + if height > width: + new_height = 2000 + new_width = int(width * 2000 / height) + else: + new_width = 2000 + new_height = int(height * 2000 / width) + img = img.resize((new_width, new_height), Image.LANCZOS) + buffered = BytesIO() img.save(buffered, format="JPEG", quality=95) img_str = base64.b64encode(buffered.getvalue()).decode()