//  [python]
def resize_and_pad(
    frames: tf.Tensor,
    max_resize: int,
    pad_size: int,
    random: bool = False,
    seed: Optional[int] = None,
    state: Optional[MutableMapping[str, tf.Tensor]] = None) -> tf.Tensor:
  """Resizes the largest and pads frames.

  Args:
    frames: A Tensor of dimension [timesteps, input_h, input_w, channels].
    max_resize: Maximum size of the final image dimensions.
    pad_size: Pad size of the final image dimensions.
    random: If true, perform random crop; otherwise, perform central crop.
    seed: Random seed.
    state: The dictionary contains random state.
  Returns:
    A Tensor of shape [timesteps, output_h, output_w, channels] of type
      frames.dtype where min(output_h, output_w) = max_resize.
  """
  if max_resize > pad_size:
    raise ValueError('max_resize should not be larger than pad_size. Got '
                     f'({max_resize}, {pad_size}).')

  pad_color = tf.reduce_mean(frames, axis=[0, 1, 2])

  shape = tf.shape(input=frames)
  image_size = tf.cast(shape[1:3], tf.float32)
  input_h = image_size[0]
  input_w = image_size[1]

  scale = tf.cast(max_resize / input_h, tf.float32)
  scale = tf.minimum(scale, tf.cast(max_resize / input_w, tf.float32))

  scale_h = input_h * scale
  scale_w = input_w * scale

  frames_resized = tf.image.resize(
      frames, (scale_h, scale_w), method=tf.image.ResizeMethod.BILINEAR)
  frames = tf.cast(frames_resized, frames.dtype)

  shape = tf.shape(input=frames)
  image_size = tf.cast(shape[1:3], tf.float32)
  size = tf.convert_to_tensor(value=(pad_size, pad_size))
  if random:
    # Limit of possible offset in order to fit the entire crop:
    # [target_h - input_h + 1, target_w - input_w + 1].
    limit = size - tf.cast(image_size, tf.int32) + 1
    offset = tf.random.uniform(
        shape=(2,),
        dtype=tf.int32,
        maxval=tf.int32.max,
        seed=seed) % limit  # [offset_h, offset_w]
    offset_height, offset_width = offset[0], offset[1]
  else:
    # Central spatial pad.
    offset_height = tf.cast((pad_size - image_size[0]) / 2, tf.int32)
    offset_width = tf.cast((pad_size - image_size[1]) / 2, tf.int32)
    offset = tf.convert_to_tensor([offset_height, offset_width])

  padded_frames = tf.image.pad_to_bounding_box(
      frames,
      offset_height=offset_height,
      offset_width=offset_width,
      target_height=pad_size,
      target_width=pad_size)

  # Setting color of the padded pixels
  frames_ones = tf.ones_like(frames)
  frames_ones_padded = tf.image.pad_to_bounding_box(
      frames_ones,
      offset_height=offset_height,
      offset_width=offset_width,
      target_height=pad_size,
      target_width=pad_size)
  frames_color_padded = (1 - frames_ones_padded) * pad_color
  padded_frames += frames_color_padded

  if state is not None:
    # Note: image_info encodes the information of the image and the applied
    # preprocessing. It is in the format of
    # [[original_height, original_width], [desired_height, desired_width],
    #  [y_scale, x_scale], [y_offset, x_offset]],
    # where [desired_height, desired_width] is the actual scaled image size,
    # and [y_scale, x_scale] is the scaling factor, which is the ratio of scaled
    # dimension / original dimension.
    image_info = tf.stack([
        tf.convert_to_tensor((input_h, input_w), tf.float32),
        tf.convert_to_tensor((pad_size, pad_size), tf.float32),
        tf.convert_to_tensor((scale, scale), tf.float32),
        # set to -1. * offset because it's padding.
        -1. * tf.cast(offset, tf.float32)])

    if 'image_info' not in state:
      state['image_info'] = image_info
    else:
      update_image_info(state, image_info)
  return padded_frames