code: def pad(
      image: np.ndarray,
      padding: Union[int, Tuple[int, int], Iterable[Tuple[int, int]]],
      mode: PaddingMode = PaddingMode.CONSTANT,
      constant_values: Union[float, Iterable[float]] = 0.0,
      data_format: Optional[Union[str, ChannelDimension]] = None,
      input_data_format: Optional[Union[str, ChannelDimension]] = None,
  ) -> np.ndarray:
      """
      Pads the `image` with the specified (height, width) `padding` and `mode`.
  
      Args:
          image (`np.ndarray`):
              The image to pad.
          padding (`int` or `Tuple[int, int]` or `Iterable[Tuple[int, int]]`):
              Padding to apply to the edges of the height, width axes. Can be one of three formats:
              - `((before_height, after_height), (before_width, after_width))` unique pad widths for each axis.
              - `((before, after),)` yields same before and after pad for height and width.
              - `(pad,)` or int is a shortcut for before = after = pad width for all axes.
          mode (`PaddingMode`):
              The padding mode to use. Can be one of:
                  - `"constant"`: pads with a constant value.
                  - `"reflect"`: pads with the reflection of the vector mirrored on the first and last values of the
                    vector along each axis.
                  - `"replicate"`: pads with the replication of the last value on the edge of the array along each axis.
                  - `"symmetric"`: pads with the reflection of the vector mirrored along the edge of the array.
          constant_values (`float` or `Iterable[float]`, *optional*):
              The value to use for the padding if `mode` is `"constant"`.
          data_format (`str` or `ChannelDimension`, *optional*):
              The channel dimension format for the output image. Can be one of:
                  - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                  - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
              If unset, will use same as the input image.
          input_data_format (`str` or `ChannelDimension`, *optional*):
              The channel dimension format for the input image. Can be one of:
                  - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                  - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
              If unset, will use the inferred format of the input image.
  
      Returns:
          `np.ndarray`: The padded image.
  
      """
      if input_data_format is None:
          input_data_format = infer_channel_dimension_format(image)
  
      def _expand_for_data_format(values):
          """
          Convert values to be in the format expected by np.pad based on the data format.
          """
          if isinstance(values, (int, float)):
              values = ((values, values), (values, values))
          elif isinstance(values, tuple) and len(values) == 1:
              values = ((values[0], values[0]), (values[0], values[0]))
          elif (
              isinstance(values, tuple)
              and len(values) == 2
              and isinstance(values[0], int)
          ):
              values = (values, values)
          elif (
              isinstance(values, tuple)
              and len(values) == 2
              and isinstance(values[0], tuple)
          ):
              values = values
          else:
              raise ValueError(f"Unsupported format: {values}")
  
          # add 0 for channel dimension
          values = (
              ((0, 0), *values)
              if input_data_format == ChannelDimension.FIRST
              else (*values, (0, 0))
          )
  
          # Add additional padding if there's a batch dimension
          values = (0, *values) if image.ndim == 4 else values
          return values
  
      padding = _expand_for_data_format(padding)
  
      if mode == PaddingMode.CONSTANT:
          constant_values = _expand_for_data_format(constant_values)
          image = np.pad(
              image,
              padding,
              mode="constant",
              constant_values=constant_values,
          )
      elif mode == PaddingMode.REFLECT:
          image = np.pad(image, padding, mode="reflect")
      elif mode == PaddingMode.REPLICATE:
          image = np.pad(image, padding, mode="edge")
      elif mode == PaddingMode.SYMMETRIC:
          image = np.pad(image, padding, mode="symmetric")
      else:
          raise ValueError(f"Invalid padding mode: {mode}")
  
      image = (
          to_channel_dimension_format(
              image, data_format, input_data_format
          )
          if data_format is not None
          else image
      )
      return image
  
imports: import warnings
  from typing import Iterable, List, Optional, Tuple, Union
  import numpy
  from .image_utils import ChannelDimension, ImageInput, get_channel_dimension_axis, get_image_size, infer_channel_dimension_format
  from .utils import ExplicitEnum, TensorType, is_jax_tensor, is_tf_tensor, is_torch_tensor
  from .utils.import_utils import is_flax_available, is_tf_available, is_torch_available, is_vision_available, requires_backends
label_desc: Use image node_id 19 for calling a global function with example usage: pad(image, padding, mode, constant_values, data_format, input_data_format) and returns: image, values