Skip to main content

Function Signature

Description

Loads an image from various sources (local file, URL, base64 string, or PIL.Image object) and applies smart resizing based on the model’s requirements. The function automatically handles different image formats and converts them to RGB.

Parameters

Dict[str, Union[str, Image.Image]]
required
Dictionary containing image information and optional resize parameters.Required keys:
  • image or image_url: The image source (file path, URL, base64 string, or PIL.Image)
Optional keys:
  • resized_height: Target height for resizing
  • resized_width: Target width for resizing
  • min_pixels: Minimum number of pixels (default: 4 * patch_factor²)
  • max_pixels: Maximum number of pixels (default: 16384 * patch_factor²)
int
default:"14"
The patch size used by the vision encoder. Affects the resizing factor calculation.Common values:
  • 14 for Qwen2VL and Qwen2.5VL
  • 16 for Qwen3VL
The actual resize factor is image_patch_size * 2 (spatial merge size).

Returns

PIL.Image.Image
Processed RGB PIL Image resized to dimensions divisible by the patch factor.The image dimensions are calculated using smart_resize to maintain aspect ratio while staying within min/max pixel constraints.

Supported Image Sources

Local File Path

HTTP/HTTPS URL

Base64 Encoded

PIL.Image Object

Custom Resize Parameters

Specify Exact Dimensions

Note: The actual dimensions will be adjusted to be divisible by the patch factor.

Control Pixel Range

Image Processing Steps

  1. Load Image: Detects source type (file, URL, base64, PIL) and loads the image
  2. Convert to RGB: Handles RGBA images by compositing on white background
  3. Calculate Resize Dimensions: Uses smart_resize to determine optimal dimensions
  4. Resize: Applies resize maintaining aspect ratio within constraints

Error Handling

Usage with process_vision_info

While you can use fetch_image directly, it’s typically called internally by process_vision_info:

RGBA Image Handling

Images with alpha channels (RGBA) are automatically composited onto a white background:

See Also