From 3e86d81fc6e2daeaec4b6a626e82507b2a9b6c66 Mon Sep 17 00:00:00 2001 From: Jaret Burkett Date: Thu, 28 May 2026 09:36:17 -0600 Subject: [PATCH] Adjust bucket sizes to achieve maximum pixels without going over. --- toolkit/buckets.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/toolkit/buckets.py b/toolkit/buckets.py index 145b193d..b6105786 100644 --- a/toolkit/buckets.py +++ b/toolkit/buckets.py @@ -1,3 +1,4 @@ +import math from typing import TypedDict @@ -22,7 +23,26 @@ def get_bucket_for_image_size( target_pixels = min(total_pixels, max_pixels) scaler = target_pixels / total_pixels - new_width = int(round((width * scaler) / divisibility) * divisibility) - new_height = int(round((height * scaler) / divisibility) * divisibility) + w_raw = (width * scaler) / divisibility + h_raw = (height * scaler) / divisibility + + candidates = [ + (math.floor(w_raw) * divisibility, math.floor(h_raw) * divisibility), + (math.floor(w_raw) * divisibility, math.ceil(h_raw) * divisibility), + (math.ceil(w_raw) * divisibility, math.floor(h_raw) * divisibility), + (math.ceil(w_raw) * divisibility, math.ceil(h_raw) * divisibility), + ] + capped = [(w, h) for w, h in candidates if w > 0 and h > 0 and w * h <= max_pixels] + if not capped: + capped = [ + ( + max(divisibility, math.floor(w_raw) * divisibility), + max(divisibility, math.floor(h_raw) * divisibility), + ) + ] + + new_width, new_height = min( + capped, key=lambda wh: abs(wh[0] * wh[1] - target_pixels) + ) return {"width": new_width, "height": new_height}