Skip to content

vllm.multimodal.image

Functions:

  • get_image_id_bytes –

    Return the EXIF ImageID of an image as a UUID, if it carries one.

  • normalize_image –

    Normalize EXIF orientation so the pixel data matches visual display.

  • rescale_image_size –

    Rescale the dimensions of an image by a constant factor.

  • rgba_to_rgb –

    Convert an RGBA image to RGB with filled background color.

_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", {})

get_image_id_bytes(image)

Return the EXIF ImageID of an image as a UUID, if it carries one.

An ImageID identifies the logical image, so callers use it as the cache identity: the same image under different encodings, dimensions or quantization then shares one entry. Malformed EXIF (e.g. an invalid TIFF header) yields None instead of raising, leaving the caller to fall back to the encoded bytes.

Only pass an image whose pixels are already decoded or whose header was parsed without decoding: getexif() rasterizes a PNG that has no exif entry in info (a trailing eXIf chunk is only findable by reading to EOF).

Source code in vllm/multimodal/image.py
def get_image_id_bytes(image: Image.Image) -> bytes | None:
    """Return the EXIF `ImageID` of an image as a UUID, if it carries one.

    An `ImageID` identifies the logical image, so callers use it as the cache
    identity: the same image under different encodings, dimensions or
    quantization then shares one entry. Malformed EXIF (e.g. an invalid TIFF
    header) yields `None` instead of raising, leaving the caller to fall back
    to the encoded bytes.

    Only pass an image whose pixels are already decoded or whose header was
    parsed without decoding: `getexif()` rasterizes a PNG that has no `exif`
    entry in `info` (a trailing eXIf chunk is only findable by reading to EOF).
    """
    try:
        exif = image.getexif()
        image_id = exif.get(Image.ExifTags.Base.ImageID)
        if isinstance(image_id, uuid.UUID):
            return image_id.bytes
    except Exception:
        # Tolerate malformed EXIF metadata (e.g. invalid TIFF header).
        pass
    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

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