Skip to content

vllm.multimodal.image

Functions:

_has_transparency(image)

Detect whether an image carries transparency data (RGBA, LA, PA, or tRNS chunk in P/L/RGB PNGs).

Source code in vllm/multimodal/image.py
def _has_transparency(image: Image.Image) -> bool:
    """Detect whether an image carries transparency data (RGBA, LA, PA,
    or tRNS chunk in P/L/RGB PNGs)."""
    if image.mode in ("RGBA", "LA", "PA"):
        return True
    return "transparency" in getattr(image, "info", {})

can_decode_with_torchcodec(image, data)

Whether torchcodec decodes data, opened by Pillow as image, to the same pixels as convert_image_mode(normalize_image(image), "RGB").

Source code in vllm/multimodal/image.py
def can_decode_with_torchcodec(image: Image.Image, data: bytes) -> bool:
    """Whether torchcodec decodes `data`, opened by Pillow as `image`, to the
    same pixels as `convert_image_mode(normalize_image(image), "RGB")`."""
    return (
        image.format in _TORCHCODEC_FORMATS
        and image.mode in _TORCHCODEC_MODES
        and not _has_transparency(image)
        and not getattr(image, "is_animated", False)
        and not _is_16_bit_png(image, data)
        and _orientation_visible_to_torchcodec(image, data)
    )

decode_image_torchcodec(data)

Decode data to a CHW uint8 RGB tensor, applying the EXIF orientation like normalize_image. Returns None if torchcodec fails, so the caller can fall back to Pillow: it either decodes the image (e.g. a codec left out of a source build of torchcodec) or raises the usual client error.

Source code in vllm/multimodal/image.py
def decode_image_torchcodec(data: bytes) -> torch.Tensor | None:
    """Decode `data` to a CHW uint8 RGB tensor, applying the EXIF orientation
    like `normalize_image`. Returns None if torchcodec fails, so the caller
    can fall back to Pillow: it either decodes the image (e.g. a codec left out
    of a source build of torchcodec) or raises the usual client error."""
    try:
        return torchcodec_decode_image(data, mode="RGB")
    except Exception as e:
        logger.warning_once(
            "torchcodec failed to decode an image (%s); falling back to Pillow.",
            type(e).__name__,
        )
        return None

normalize_image(image)

Normalize EXIF orientation so the pixel data matches visual display.

Source code in vllm/multimodal/image.py
def normalize_image(image: Image.Image) -> Image.Image:
    """Normalize EXIF orientation so the pixel data matches visual display."""
    with contextlib.suppress(Exception):
        image = ImageOps.exif_transpose(image)
    return image

open_image(fp)

Open image bytes/files with the serving format allowlist.

Source code in vllm/multimodal/image.py
def open_image(fp: Any) -> Image.Image:
    """Open image bytes/files with the serving format allowlist."""
    return Image.open(fp, formats=ALLOWED_IMAGE_FORMATS)

rescale_image_size(image, size_factor, transpose=-1)

Rescale the dimensions of an image by a constant factor.

Source code in vllm/multimodal/image.py
def rescale_image_size(
    image: Image.Image, size_factor: float, transpose: int = -1
) -> Image.Image:
    """Rescale the dimensions of an image by a constant factor."""
    new_width = int(image.width * size_factor)
    new_height = int(image.height * size_factor)
    image = image.resize((new_width, new_height))
    if transpose >= 0:
        image = image.transpose(Image.Transpose(transpose))
    return image

rgba_to_rgb(image, background_color=(255, 255, 255))

Convert an RGBA image to RGB with filled background color.

Source code in vllm/multimodal/image.py
def rgba_to_rgb(
    image: Image.Image,
    background_color: tuple[int, int, int] | list[int] = (255, 255, 255),
) -> Image.Image:
    """Convert an RGBA image to RGB with filled background color."""
    assert image.mode == "RGBA"
    converted = Image.new("RGB", image.size, background_color)
    converted.paste(image, mask=image.split()[3])  # 3 is the alpha channel
    return converted