code: def resize_and_pad_to_given_size(
      src: Image.Image, w: int, h: int, crop: bool
  ) -> Image.Image:
      """
      Returns the image src resized & padded with black if needed for the screenshot
      transformation (i.e. if the spot for the image in the template is too small or
      too big for the src image). If crop is True, will crop the src image if necessary
      to fit into the template image; otherwise, will resize if necessary
      """
      curr_w, curr_h = src.size
      if crop:
          dx = (curr_w - w) // 2
          dy = (curr_h - h) // 2
          src = src.crop((dx, dy, w + dx, h + dy))
          curr_w, curr_h = src.size
      elif curr_w > w or curr_h > h:
          resize_factor = min(w / curr_w, h / curr_h)
          new_w = int(curr_w * resize_factor)
          new_h = int(curr_h * resize_factor)
          src = src.resize((new_w, new_h), resample=Image.BILINEAR)
          curr_w, curr_h = src.size
      if curr_w < w or curr_h < h:
          src = pad_with_black(src, w, h)
      return src
  
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 7 for calling a global function with example usage: resize_and_pad_to_given_size(src, w, h, crop) and returns: src