Module agents.images

Functions

def encode(frame: av.VideoFrame,
options: EncodeOptions) ‑> bytes
Expand source code
def encode(frame: av.VideoFrame, options: EncodeOptions) -> bytes:
    """Encode with optimized pipeline"""
    img = frame.to_image()
    
    
    if options.resize_options:
        img = img.resize(
            (options.resize_options.width, options.resize_options.height),
            resample=PILImage.Resampling.LANCZOS
        )
    
    
    buffer = io.BytesIO()
    img.save(buffer,
            format=options.format,
            quality=options.quality,
            optimize=True,  
            subsampling=0,  
            qtables="web_high"
    )
    return buffer.getvalue()

Encode with optimized pipeline

Classes

class EncodeOptions (format: "Literal['JPEG', 'PNG']" = 'JPEG',
resize_options: ResizeOptions = <factory>,
quality: int = 90)
Expand source code
@dataclass
class EncodeOptions:
    """Configuration settings for converting av.VideoFrame into standard image formats."""

    format: Literal["JPEG", "PNG"] = "JPEG"
    """The encoding format for the image."""

    resize_options: ResizeOptions = field(default_factory=lambda: ResizeOptions(
        width=320,  
        height=240
    ))
    """Settings for adjusting the image size."""

    quality: int = 90 
    """Compression level for the image, ranging from 0 to 100. Applicable only to JPEG."""

Configuration settings for converting av.VideoFrame into standard image formats.

Instance variables

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

The encoding format for the image.

var quality : int

Compression level for the image, ranging from 0 to 100. Applicable only to JPEG.

var resize_optionsResizeOptions

Settings for adjusting the image size.

class ResizeOptions (width: int, height: int)
Expand source code
@dataclass
class ResizeOptions:
    """Configuration for resizing av.VideoFrame during the process of encoding to a standard image format."""

    width: int
    """The target width for resizing"""

    height: int
    """The target height for resizing the image."""

Configuration for resizing av.VideoFrame during the process of encoding to a standard image format.

Instance variables

var height : int

The target height for resizing the image.

var width : int

The target width for resizing