code: def downsample_image(
      image: Image.Image,
      new_size: tuple[int, int],
      *,
      resample_mode: Image.Resampling = Image.Resampling.BICUBIC,
      reducing_gap: int = 3,
  ) -> Image.Image:
      """Downsample an image to fit within the given limits.
  
      The DPI is adjusted to match the new size, which is how we can ensure the
      OCR is positioned correctly.
  
      Args:
          image: The image to downsample
          new_size: The new size of the image.
          resample_mode: The resampling mode to use when downsampling.
          reducing_gap: The reducing gap to use when downsampling (for larger
              reductions).
      """
      if new_size == image.size:
          return image
  
      original_size = image.size
      original_dpi = image.info["dpi"]
      image = image.resize(
          new_size,
          resample=resample_mode,
          reducing_gap=reducing_gap,
      )
      image.info["dpi"] = (
          round(original_dpi[0] * new_size[0] / original_size[0]),
          round(original_dpi[1] * new_size[1] / original_size[1]),
      )
      log.debug(
          f"Rescaled image to {image.size} pixels and {image.info['dpi']} dpi"
      )
      return image
  
imports: from __future__ import annotations
  import logging
  from math import floor, sqrt
  from PIL import Image
label_desc: Use image node_id 4 for calling a global function with example usage: downsample_image(image, new_size) and returns: image, image