Module livekit.agents.utils.images

Sub-modules

livekit.agents.utils.images.image

Functions

def encode(frame: VideoFrame,
options: EncodeOptions) ‑> bytes
Expand source code
def encode(frame: rtc.VideoFrame, options: EncodeOptions) -> bytes:
    """Encode a rtc.VideoFrame to a portable image format (JPEG or PNG).

    See EncodeOptions for more details.
    """
    import_pil()
    img = _image_from_frame(frame)
    resized = _resize_image(img, options)
    buffer = io.BytesIO()
    kwargs = {}
    if options.format == "JPEG" and options.quality is not None:
        kwargs["quality"] = options.quality
    resized.save(buffer, options.format, **kwargs)
    buffer.seek(0)
    return buffer.read()

Encode a rtc.VideoFrame to a portable image format (JPEG or PNG).

See EncodeOptions for more details.

Classes

class EncodeOptions (format: Literal['JPEG', 'PNG'] = 'JPEG',
resize_options: ForwardRef('ResizeOptions') | None = None,
quality: int | None = 75)
Expand source code
@dataclass
class EncodeOptions:
    """Options for encoding rtc.VideoFrame to portable image formats."""

    format: Literal["JPEG", "PNG"] = "JPEG"
    """The format to encode the image."""

    resize_options: Optional["ResizeOptions"] = None
    """Options for resizing the image."""

    quality: Optional[int] = 75
    """Image compression quality, 0-100. Only applies to JPEG."""

Options for encoding rtc.VideoFrame to portable image formats.

Instance variables

var format : Literal['JPEG', 'PNG']

The format to encode the image.

var quality : int | None

Image compression quality, 0-100. Only applies to JPEG.

var resize_optionsResizeOptions | None

Options for resizing the image.

class ResizeOptions (width: int,
height: int,
strategy: Literal['center_aspect_fit', 'center_aspect_cover', 'scale_aspect_fit', 'scale_aspect_cover', 'skew'])
Expand source code
@dataclass
class ResizeOptions:
    """Options for resizing rtc.VideoFrame as part of encoding to a portable image format."""

    width: int
    """The desired resize width (in)"""

    height: int
    """The desired height to resize the image to."""

    strategy: Literal[
        "center_aspect_fit",
        "center_aspect_cover",
        "scale_aspect_fit",
        "scale_aspect_cover",
        "skew",
    ]
    """The strategy to use when resizing the image:
    - center_aspect_fit: Fit the image into the provided dimensions, with letterboxing
    - center_aspect_cover: Fill the provided dimensions, with cropping
    - scale_aspect_fit: Fit the image into the provided dimensions, preserving its original aspect ratio
    - scale_aspect_cover: Fill the provided dimensions, preserving its original aspect ratio (image will be larger than the provided dimensions)
    - skew: Precisely resize the image to the provided dimensions
    """

Options for resizing rtc.VideoFrame as part of encoding to a portable image format.

Instance variables

var height : int

The desired height to resize the image to.

var strategy : Literal['center_aspect_fit', 'center_aspect_cover', 'scale_aspect_fit', 'scale_aspect_cover', 'skew']

The strategy to use when resizing the image: - center_aspect_fit: Fit the image into the provided dimensions, with letterboxing - center_aspect_cover: Fill the provided dimensions, with cropping - scale_aspect_fit: Fit the image into the provided dimensions, preserving its original aspect ratio - scale_aspect_cover: Fill the provided dimensions, preserving its original aspect ratio (image will be larger than the provided dimensions) - skew: Precisely resize the image to the provided dimensions

var width : int

The desired resize width (in)