MCPcopy
hub / github.com/pytorch/vision / pad_image

Function pad_image

torchvision/transforms/v2/functional/_geometry.py:1563–1588  ·  view source on GitHub ↗
(
    image: torch.Tensor,
    padding: list[int],
    fill: Optional[Union[int, float, list[float]]] = None,
    padding_mode: str = "constant",
)

Source from the content-addressed store, hash-verified

1561@_register_kernel_internal(pad, torch.Tensor)
1562@_register_kernel_internal(pad, tv_tensors.Image)
1563def pad_image(
1564 image: torch.Tensor,
1565 padding: list[int],
1566 fill: Optional[Union[int, float, list[float]]] = None,
1567 padding_mode: str = "constant",
1568) -> torch.Tensor:
1569 # Be aware that while `padding` has order `[left, top, right, bottom]`, `torch_padding` uses
1570 # `[left, right, top, bottom]`. This stems from the fact that we align our API with PIL, but need to use `torch_pad`
1571 # internally.
1572 torch_padding = _parse_pad_padding(padding)
1573
1574 if padding_mode not in ("constant", "edge", "reflect", "symmetric"):
1575 raise ValueError(
1576 f"`padding_mode` should be either `'constant'`, `'edge'`, `'reflect'` or `'symmetric'`, "
1577 f"but got `'{padding_mode}'`."
1578 )
1579
1580 if fill is None:
1581 fill = 0
1582
1583 if isinstance(fill, (int, float)):
1584 return _pad_with_scalar_fill(image, torch_padding, fill=fill, padding_mode=padding_mode)
1585 elif len(fill) == 1:
1586 return _pad_with_scalar_fill(image, torch_padding, fill=fill[0], padding_mode=padding_mode)
1587 else:
1588 return _pad_with_vector_fill(image, torch_padding, fill=fill, padding_mode=padding_mode)
1589
1590
1591def _pad_with_scalar_fill(

Callers 3

padFunction · 0.85
pad_maskFunction · 0.85
pad_videoFunction · 0.85

Calls 3

_pad_with_scalar_fillFunction · 0.85
_pad_with_vector_fillFunction · 0.85
_parse_pad_paddingFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…