code: def get_paddings(
      image_shape: tf.Tensor,
      size: Union[int, Tuple[int, int], Sequence[int]],
      pre_spatial_dim: Optional[int] = None,
      allow_crop: bool = True,
      mode: str = "bottom_right",
  ) -> tf.Tensor:
      """Returns paddings tensors for tf.pad operation.
  
      Args:
        image_shape: The shape of the Tensor to be padded. The shape can be
          [..., N, H, W, C] or [..., H, W, C]. The paddings are computed for H, W
          and optionally N dimensions.
        size: The total size for the H and W dimensions to pad to.
        pre_spatial_dim: Optional, additional padding dimension before the spatial
          dimensions. It is only used if given and if len(shape) > 3.
        allow_crop: If size is bigger than requested max size, padding will be
          negative. If allow_crop is true, negative padding values will be set to 0.
        mode: Padding mode, "bottom_right" or "central".
  
      Returns:
        Paddings the given tensor shape.
      """
      assert image_shape.shape.rank == 1
      if isinstance(size, int):
          size = (size, size)
      h, w = image_shape[-3], image_shape[-2]
      if mode == "bottom_right":
          top, left = 0, 0
      elif mode == "central":
          top, left = (size[0] - h) // 2, (size[1] - w) // 2
      else:
          raise ValueError(f"Unknown padding mode: {mode}")
      # Spatial padding.
      paddings = [
          tf.stack([top, size[0] - h - top]),
          tf.stack([left, size[1] - w - left]),
          tf.stack([0, 0]),
      ]
      ndims = image_shape.shape[0]  # pytype: disable=wrong-arg-types
      # Prepend padding for temporal dimension or number of instances.
      if pre_spatial_dim is not None and ndims > 3:
          paddings = [[0, pre_spatial_dim - image_shape[-4]]] + paddings
      # Prepend with non-padded dimensions if available.
      if ndims > len(paddings):
          paddings = [[0, 0]] * (ndims - len(paddings)) + paddings
      if allow_crop:
          paddings = tf.maximum(paddings, 0)
      return tf.stack(paddings)
  
file_path: scenic/scenic/projects/owl_vit/preprocessing/transforms.py
imports: from typing import Any, Optional, Sequence, Tuple, Union
  import tensorflow
label_desc: Use image node_id 9 for calling a global function with example usage: get_paddings(image_shape, size, pre_spatial_dim, allow_crop, mode) and returns: tf