Skip to content

vllm.multimodal.image

ImageEmbeddingMediaIO

Bases: MediaIO[Tensor]

Source code in vllm/multimodal/image.py
class ImageEmbeddingMediaIO(MediaIO[torch.Tensor]):

    def __init__(self) -> None:
        super().__init__()

    def load_bytes(self, data: bytes) -> torch.Tensor:
        buffer = BytesIO(data)
        return torch.load(buffer, weights_only=True)

    def load_base64(self, media_type: str, data: str) -> torch.Tensor:
        return self.load_bytes(pybase64.b64decode(data, validate=True))

    def load_file(self, filepath: Path) -> torch.Tensor:
        return torch.load(filepath, weights_only=True)

    def encode_base64(self, media: torch.Tensor) -> str:
        return pybase64.b64encode(media.numpy()).decode('utf-8')

__init__

__init__() -> None
Source code in vllm/multimodal/image.py
def __init__(self) -> None:
    super().__init__()

encode_base64

encode_base64(media: Tensor) -> str
Source code in vllm/multimodal/image.py
def encode_base64(self, media: torch.Tensor) -> str:
    return pybase64.b64encode(media.numpy()).decode('utf-8')

load_base64

load_base64(media_type: str, data: str) -> Tensor
Source code in vllm/multimodal/image.py
def load_base64(self, media_type: str, data: str) -> torch.Tensor:
    return self.load_bytes(pybase64.b64decode(data, validate=True))

load_bytes

load_bytes(data: bytes) -> Tensor
Source code in vllm/multimodal/image.py
def load_bytes(self, data: bytes) -> torch.Tensor:
    buffer = BytesIO(data)
    return torch.load(buffer, weights_only=True)

load_file

load_file(filepath: Path) -> Tensor
Source code in vllm/multimodal/image.py
def load_file(self, filepath: Path) -> torch.Tensor:
    return torch.load(filepath, weights_only=True)

ImageMediaIO

Bases: MediaIO[Image]

Source code in vllm/multimodal/image.py
class ImageMediaIO(MediaIO[Image.Image]):

    def __init__(self, image_mode: str = "RGB", **kwargs) -> None:
        super().__init__()

        self.image_mode = image_mode
        # `kwargs` contains custom arguments from
        # --media-io-kwargs for this modality.
        # They can be passed to the underlying
        # media loaders (e.g. custom implementations)
        # for flexible control.
        self.kwargs = kwargs

    def load_bytes(self, data: bytes) -> Image.Image:
        image = Image.open(BytesIO(data))
        image.load()
        return convert_image_mode(image, self.image_mode)

    def load_base64(self, media_type: str, data: str) -> Image.Image:
        return self.load_bytes(pybase64.b64decode(data, validate=True))

    def load_file(self, filepath: Path) -> Image.Image:
        image = Image.open(filepath)
        image.load()
        return convert_image_mode(image, self.image_mode)

    def encode_base64(
        self,
        media: Image.Image,
        *,
        image_format: str = "JPEG",
    ) -> str:
        image = media

        with BytesIO() as buffer:
            image = convert_image_mode(image, self.image_mode)
            image.save(buffer, image_format)
            data = buffer.getvalue()

        return pybase64.b64encode(data).decode('utf-8')

image_mode instance-attribute

image_mode = image_mode

kwargs instance-attribute

kwargs = kwargs

__init__

__init__(image_mode: str = 'RGB', **kwargs) -> None
Source code in vllm/multimodal/image.py
def __init__(self, image_mode: str = "RGB", **kwargs) -> None:
    super().__init__()

    self.image_mode = image_mode
    # `kwargs` contains custom arguments from
    # --media-io-kwargs for this modality.
    # They can be passed to the underlying
    # media loaders (e.g. custom implementations)
    # for flexible control.
    self.kwargs = kwargs

encode_base64

encode_base64(
    media: Image, *, image_format: str = "JPEG"
) -> str
Source code in vllm/multimodal/image.py
def encode_base64(
    self,
    media: Image.Image,
    *,
    image_format: str = "JPEG",
) -> str:
    image = media

    with BytesIO() as buffer:
        image = convert_image_mode(image, self.image_mode)
        image.save(buffer, image_format)
        data = buffer.getvalue()

    return pybase64.b64encode(data).decode('utf-8')

load_base64

load_base64(media_type: str, data: str) -> Image
Source code in vllm/multimodal/image.py
def load_base64(self, media_type: str, data: str) -> Image.Image:
    return self.load_bytes(pybase64.b64decode(data, validate=True))

load_bytes

load_bytes(data: bytes) -> Image
Source code in vllm/multimodal/image.py
def load_bytes(self, data: bytes) -> Image.Image:
    image = Image.open(BytesIO(data))
    image.load()
    return convert_image_mode(image, self.image_mode)

load_file

load_file(filepath: Path) -> Image
Source code in vllm/multimodal/image.py
def load_file(self, filepath: Path) -> Image.Image:
    image = Image.open(filepath)
    image.load()
    return convert_image_mode(image, self.image_mode)

convert_image_mode

convert_image_mode(image: Image, to_mode: str)
Source code in vllm/multimodal/image.py
def convert_image_mode(image: Image.Image, to_mode: str):
    if image.mode == to_mode:
        return image
    elif image.mode == "RGBA" and to_mode == "RGB":
        return rgba_to_rgb(image)
    else:
        return image.convert(to_mode)

rescale_image_size

rescale_image_size(
    image: Image, size_factor: float, transpose: int = -1
) -> Image

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

rgba_to_rgb(
    image: Image, background_color=(255, 255, 255)
) -> Image

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=(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