From 74c9beca7d826189c78b9f5445a7319d21b4cafc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=83=81=EC=97=B0?= Date: Thu, 30 Apr 2026 10:07:19 +0900 Subject: [PATCH] fix: preserve aspect ratio in scale_to_fit grid quantization Independent rounding of w_blocks and h_blocks can push them in opposite directions (one up, one down), distorting the aspect ratio. For A4 at 192 DPI this produces 1.403 instead of 1.4142. Derive h_blocks from the already-rounded w_blocks so both axes are quantized consistently. --- chandra/model/util.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/chandra/model/util.py b/chandra/model/util.py index f7e97a3..0133b17 100644 --- a/chandra/model/util.py +++ b/chandra/model/util.py @@ -30,8 +30,11 @@ def scale_to_fit( scale = (min_pixels / current_pixels) ** 0.5 # 2. Convert dimensions to integer "grid blocks" + # Derive h_blocks from rounded w_blocks to preserve aspect ratio. + # Independent rounding of both axes can push them in opposite directions + # (e.g. w rounds up, h rounds down), distorting the ratio. w_blocks = max(1, round((width * scale) / grid_size)) - h_blocks = max(1, round((height * scale) / grid_size)) + h_blocks = max(1, round(w_blocks * (height / width))) # 3. Refinement Loop: Ensure we are under the max limit while (w_blocks * h_blocks * grid_size * grid_size) > max_pixels: