mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-28 06:51:16 +08:00
fix(vision): preserve aspect ratio during auto-resize
Independent halving of width and height caused aspect ratio distortion for extreme dimensions (e.g. 8000x200 panoramas). When one axis hit the 64px floor, the other kept shrinking — collapsing the ratio toward 1:1. Use proportional scaling instead: when either dimension hits the floor, derive the effective scale factor and apply it to both axes. Add tests for extreme panorama (8000x200) and tall narrow (200x6000) images to verify aspect ratio preservation.
This commit is contained in:
@@ -357,8 +357,19 @@ def _resize_image_for_vision(image_path: Path, mime_type: Optional[str] = None,
|
||||
|
||||
for attempt in range(5):
|
||||
if attempt > 0:
|
||||
new_w = max(img.width // 2, 64)
|
||||
new_h = max(img.height // 2, 64)
|
||||
# Proportional scaling: halve the longer side and scale the
|
||||
# shorter side to preserve aspect ratio (min dimension 64).
|
||||
scale = 0.5
|
||||
new_w = max(int(img.width * scale), 64)
|
||||
new_h = max(int(img.height * scale), 64)
|
||||
# Re-derive the scale from whichever dimension hit the floor
|
||||
# so both axes shrink by the same factor.
|
||||
if new_w == 64 and img.width > 0:
|
||||
effective_scale = 64 / img.width
|
||||
new_h = max(int(img.height * effective_scale), 64)
|
||||
elif new_h == 64 and img.height > 0:
|
||||
effective_scale = 64 / img.height
|
||||
new_w = max(int(img.width * effective_scale), 64)
|
||||
# Stop if dimensions can't shrink further
|
||||
if (new_w, new_h) == prev_dims:
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user