cutcutcodec.core.filter.video.pad

Add padding to an image while keeping the proportions.

Functions

pad_keep_ratio(image, shape[, copy])

Pad the image with transparent borders.

Details

cutcutcodec.core.filter.video.pad.pad_keep_ratio(image: FrameVideo | Tensor | ndarray, shape: tuple[Integral, Integral] | list[Integral], copy: bool = True) FrameVideo | Tensor | ndarray[source]

Pad the image with transparent borders.

Parameters

imagecutcutcodec.core.classes.image_video.FrameVideo or torch.Tensor or numpy.ndarray

The image to be padded. If a numpy array is provide, the format has to match with the video image specifications.

shapeint and int

The pixel dimensions of the returned image. Each dimension has to be larger or equal to the provided image. The convention adopted is the numpy convention (height, width).

copyboolean, default=True

If True, ensure that the returned tensor doesn’t share the data of the input tensor.

Returns

padded_image

The padded image homogeneous with the input.

Examples

>>> import torch
>>> from cutcutcodec.core.classes.frame_video import FrameVideo
>>> from cutcutcodec.core.filter.video.pad import pad_keep_ratio
>>> ref = FrameVideo(0, torch.full((4, 6, 1), 0.5))
>>> pad_keep_ratio(ref, (8, 6))[..., 1]  # alpha layer
tensor([[0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.]])
>>> pad_keep_ratio(ref, (4, 7)).convert(1)[..., 0]  # as gray
tensor([[0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.0000],
        [0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.0000],
        [0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.0000],
        [0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.0000]])
>>> pad_keep_ratio(ref, (6, 8)).convert(1)[..., 0]  # as gray
tensor([[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
        [0.0000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.0000],
        [0.0000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.0000],
        [0.0000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.0000],
        [0.0000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.0000],
        [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]])
>>>