Function Signature
Description
Calculates optimal image dimensions that satisfy multiple constraints simultaneously:- Both dimensions are divisible by
factor - Total pixels are within
[min_pixels, max_pixels]range - Aspect ratio is maintained as closely as possible
Parameters
int
required
Original image height in pixels.
int
required
Original image width in pixels.
int
required
Both output dimensions must be divisible by this factor.Typically calculated as:
image_patch_size * SPATIAL_MERGE_SIZE- For patch size 14: factor = 28
- For patch size 16: factor = 32
Optional[int]
default:"IMAGE_MIN_TOKEN_NUM * factor²"
Minimum total pixels allowed in the resized image.Default:
4 * factor² (IMAGE_MIN_TOKEN_NUM = 4)Optional[int]
default:"IMAGE_MAX_TOKEN_NUM * factor²"
Maximum total pixels allowed in the resized image.Default:
16384 * factor² (IMAGE_MAX_TOKEN_NUM = 16384)Returns
int
New height divisible by
factor, maintaining aspect ratio within pixel constraints.int
New width divisible by
factor, maintaining aspect ratio within pixel constraints.Algorithm
The function follows this logic:- Round to factor: Round both dimensions to nearest multiple of
factor - Check max constraint: If
height * width > max_pixels:- Calculate scaling factor:
β = sqrt((height * width) / max_pixels) - Scale down:
new_height = floor(height / β / factor) * factor
- Calculate scaling factor:
- Check min constraint: If
height * width < min_pixels:- Calculate scaling factor:
β = sqrt(min_pixels / (height * width)) - Scale up:
new_height = ceil(height * β / factor) * factor
- Calculate scaling factor:
Usage Examples
Basic Usage
With Pixel Constraints
Image Tokens for Vision Models
Aspect Ratio Preservation
Error Handling
Aspect Ratio Too Extreme
Invalid Pixel Range
Constants
Real-World Examples
Common Image Sizes
Different Patch Sizes
Integration with Other Functions
Used in fetch_image
Used in fetch_video
See Also
- fetch_image - Uses smart_resize for image processing
- fetch_video - Uses smart_resize for video frame processing
- process_vision_info - High-level vision processing function