code: def resize_image(resize_mode, im, width, height, upscaler_name=None):
      """
      Resizes an image with the specified resize_mode, width, and height.
  
      Args:
          resize_mode: The mode to use when resizing the image.
              0: Resize the image to the specified width and height.
              1: Resize the image to fill the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, cropping the excess.
              2: Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, filling empty with data from image.
          im: The image to resize.
          width: The width to resize the image to.
          height: The height to resize the image to.
          upscaler_name: The name of the upscaler to use. If not provided, defaults to opts.upscaler_for_img2img.
      """
  
      upscaler_name = upscaler_name or opts.upscaler_for_img2img
  
      def resize(im, w, h):
          if (
              upscaler_name is None
              or upscaler_name == "None"
              or im.mode == "L"
          ):
              return im.resize((w, h), resample=LANCZOS)
  
          scale = max(w / im.width, h / im.height)
  
          if scale > 1.0:
              upscalers = [
                  x
                  for x in shared.sd_upscalers
                  if x.name == upscaler_name
              ]
              if len(upscalers) == 0:
                  upscaler = shared.sd_upscalers[0]
                  print(
                      f"could not find upscaler named {upscaler_name or '<empty string>'}, using {upscaler.name} as a fallback"
                  )
              else:
                  upscaler = upscalers[0]
  
              im = upscaler.scaler.upscale(
                  im, scale, upscaler.data_path
              )
  
          if im.width != w or im.height != h:
              im = im.resize((w, h), resample=LANCZOS)
  
          return im
  
      if resize_mode == 0:
          res = resize(im, width, height)
  
      elif resize_mode == 1:
          ratio = width / height
          src_ratio = im.width / im.height
  
          src_w = (
              width
              if ratio > src_ratio
              else im.width * height // im.height
          )
          src_h = (
              height
              if ratio <= src_ratio
              else im.height * width // im.width
          )
  
          resized = resize(im, src_w, src_h)
          res = Image.new("RGB", (width, height))
          res.paste(
              resized,
              box=(width // 2 - src_w // 2, height // 2 - src_h // 2),
          )
  
      else:
          ratio = width / height
          src_ratio = im.width / im.height
  
          src_w = (
              width
              if ratio < src_ratio
              else im.width * height // im.height
          )
          src_h = (
              height
              if ratio >= src_ratio
              else im.height * width // im.width
          )
  
          resized = resize(im, src_w, src_h)
          res = Image.new("RGB", (width, height))
          res.paste(
              resized,
              box=(width // 2 - src_w // 2, height // 2 - src_h // 2),
          )
  
          if ratio < src_ratio:
              fill_height = height // 2 - src_h // 2
              if fill_height > 0:
                  res.paste(
                      resized.resize(
                          (width, fill_height), box=(0, 0, width, 0)
                      ),
                      box=(0, 0),
                  )
                  res.paste(
                      resized.resize(
                          (width, fill_height),
                          box=(
                              0,
                              resized.height,
                              width,
                              resized.height,
                          ),
                      ),
                      box=(0, fill_height + src_h),
                  )
          elif ratio > src_ratio:
              fill_width = width // 2 - src_w // 2
              if fill_width > 0:
                  res.paste(
                      resized.resize(
                          (fill_width, height), box=(0, 0, 0, height)
                      ),
                      box=(0, 0),
                  )
                  res.paste(
                      resized.resize(
                          (fill_width, height),
                          box=(resized.width, 0, resized.width, height),
                      ),
                      box=(fill_width + src_w, 0),
                  )
  
      return res
  
imports: from __future__ import annotations
  import datetime
  import pytz
  import io
  import math
  import os
  from collections import namedtuple
  import re
  import numpy
  import piexif
  import piexif.helper
  from PIL import Image, ImageFont, ImageDraw, ImageColor, PngImagePlugin
  import string
  import json
  import hashlib
  from modules import sd_samplers, shared, script_callbacks, errors
  from modules.paths_internal import roboto_ttf_file
  from modules.shared import opts
label_desc: Use image node_id 7 for calling a global function with example usage: resize_image(resize_mode, im, width, height, upscaler_name) and returns: res, im, im