code: def scale_template_image(
      src_w: int,
      src_h: int,
      template_image: Image.Image,
      bbox: Tuple[int, int, int, int],
      max_image_size_pixels: Optional[int],
      crop: bool,
  ) -> Tuple[Image.Image, Tuple[int, int, int, int]]:
      """
      Return template_image, and bbox resized to fit the src image. Takes in the
      width & height of the src image plus the bounding box where the src image
      will be inserted into template_image. If the template bounding box is
      bigger than src image in both dimensions, template_image is scaled down
      such that the dimension that was closest to src_image matches, without
      changing the aspect ratio (and bbox is scaled proportionally). Similarly if
      src image is bigger than the bbox in both dimensions, template_image and
      the bbox are scaled up.
      """
      template_w, template_h = template_image.size
      left, upper, right, lower = bbox
      bbox_w, bbox_h = right - left, lower - upper
      # Scale up/down template_image & bbox
      if crop:
          resize_factor = min(src_w / bbox_w, src_h / bbox_h)
      else:
          resize_factor = max(src_w / bbox_w, src_h / bbox_h)
  
      # If a max image size is provided & the resized template image would be too large,
      # resize the template image to the max image size.
      if max_image_size_pixels is not None:
          template_size = template_w * template_h
          if template_size * resize_factor**2 > max_image_size_pixels:
              resize_factor = math.sqrt(
                  max_image_size_pixels / template_size
              )
  
      template_w = int(template_w * resize_factor)
      template_h = int(template_h * resize_factor)
      bbox_w, bbox_h = int(bbox_w * resize_factor), int(
          bbox_h * resize_factor
      )
      left, upper = int(left * resize_factor), int(
          upper * resize_factor
      )
      right, lower = left + bbox_w, upper + bbox_h
      bbox = (left, upper, right, lower)
      template_image = template_image.resize(
          (template_w, template_h), resample=Image.BILINEAR
      )
      return template_image, bbox
  
file_path: AugLy/augly/image/utils/utils.py
imports: import functools
  import json
  import math
  import os
  from typing import List, Optional, Tuple, Union
  import numpy
  from augly import utils
  from PIL import Image
label_desc: Use image node_id 8 for calling a global function with example usage: scale_template_image(src_w, src_h, template_image, bbox, max_image_size_pixels, crop) and returns: template_image,  bbox