text: def _scale_size(size, scale):
      """Rescale a size by a ratio.
  
      Args:
          size (tuple[int]): (w, h).
          scale (float): Scaling factor.
  
      Returns:
          tuple[int]: scaled size.
      """
      w, h = size
      return int(w * float(scale) + 0.5), int(h * float(scale) + 0.5)
  
  
  def rescale_size(old_size, scale, return_scale=False):
      """Calculate the new size to be rescaled to.
  
      Args:
          old_size (tuple[int]): The old size (w, h) of image.
          scale (float | tuple[int]): The scaling factor or maximum size.
              If it is a float number, then the image will be rescaled by this
              factor, else if it is a tuple of 2 integers, then the image will
              be rescaled as large as possible within the scale.
          return_scale (bool): Whether to return the scaling factor besides the
              rescaled image size.
  
      Returns:
          tuple[int]: The new rescaled image size.
      """
      w, h = old_size
      if isinstance(scale, (float, int)):
          if scale <= 0:
              raise ValueError(f'Invalid scale {scale}, must be positive.')
          scale_factor = scale
      elif isinstance(scale, tuple):
          max_long_edge = max(scale)
          max_short_edge = min(scale)
          scale_factor = min(max_long_edge / max(h, w),
                             max_short_edge / min(h, w))
      else:
          raise TypeError(
              f'Scale must be a number or tuple of int, but got {type(scale)}')
  
      new_size = _scale_size((w, h), scale_factor)
  
      if return_scale:
          return new_size, scale_factor
      else:
          return new_size
  