Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion chandra/model/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down