code: def overlay_text(
      image: Union[str, Image.Image],
      output_path: Optional[str] = None,
      text: List[Union[int, List[int]]] = utils.DEFAULT_TEXT_INDICES,
      font_file: str = utils.FONT_PATH,
      font_size: float = 0.15,
      opacity: float = 1.0,
      color: Tuple[int, int, int] = utils.RED_RGB_COLOR,
      x_pos: float = 0.0,
      y_pos: float = 0.5,
      metadata: Optional[List[Dict[str, Any]]] = None,
      bboxes: Optional[List[Tuple]] = None,
      bbox_format: Optional[str] = None,
  ) -> Image.Image:
      """
      Overlay text onto the image (by default, text is randomly overlaid)
  
      @param image: the path to an image or a variable of type PIL.Image.Image
          to be augmented
  
      @param output_path: the path in which the resulting image will be stored.
          If None, the resulting PIL Image will still be returned
  
      @param text: indices (into the file) of the characters to be overlaid. Each line of
          text is represented as a list of int indices; if a list of lists is supplied,
          multiple lines of text will be overlaid
  
      @param font_file: iopath uri to the .ttf font file
  
      @param font_size: size of the overlaid characters, calculated as
          font_size * min(height, width) of the original image
  
      @param opacity: the lower the opacity, the more transparent the overlaid text
  
      @param color: color of the overlaid text in RGB values
  
      @param x_pos: position of the overlaid text relative to the image width
  
      @param y_pos: position of the overlaid text relative to the image height
  
      @param metadata: if set to be a list, metadata about the function execution
          including its name, the source & dest width, height, etc. will be appended
          to the inputted list. If set to None, no metadata will be appended or returned
  
      @param bboxes: a list of bounding boxes can be passed in here if desired. If
          provided, this list will be modified in place such that each bounding box is
          transformed according to this function
  
      @param bbox_format: signifies what bounding box format was used in `bboxes`. Must
          specify `bbox_format` if `bboxes` is provided. Supported bbox_format values are
          "pascal_voc", "pascal_voc_norm", "coco", and "yolo"
  
      @returns: the augmented PIL Image
      """
      assert (
          0.0 <= opacity <= 1.0
      ), "Opacity must be a value in the range [0.0, 1.0]"
      assert (
          0.0 <= font_size <= 1.0
      ), "Font size must be a value in the range [0.0, 1.0]"
      assert (
          0.0 <= x_pos <= 1.0
      ), "x_pos must be a value in the range [0.0, 1.0]"
      assert (
          0.0 <= y_pos <= 1.0
      ), "y_pos must be a value in the range [0.0, 1.0]"
      utils.validate_rgb_color(color)
  
      image = imutils.validate_and_load_image(image)
  
      func_kwargs = imutils.get_func_kwargs(metadata, locals())
      src_mode = image.mode
  
      text_lists = (
          text if all(isinstance(t, list) for t in text) else [text]
      )
      assert all(isinstance(t, list) for t in text_lists) and all(
          all(
              isinstance(t, int) for t in text_l
          )  # pyre-ignore text_l is a List[int]
          for text_l in text_lists
      ), "Text must be a list of ints or a list of list of ints for multiple lines"
  
      image = image.convert("RGBA")
      width, height = image.size
  
      local_font_path = utils.pathmgr.get_local_path(font_file)
      font_size = int(min(width, height) * font_size)
      font = ImageFont.truetype(local_font_path, font_size)
  
      pkl_file = os.path.splitext(font_file)[0] + ".pkl"
      local_pkl_path = utils.pathmgr.get_local_path(pkl_file)
  
      with open(local_pkl_path, "rb") as f:
          chars = pickle.load(f)
  
      try:
          text_strs = [
              # pyre-fixme[16]: Item `int` of `Union[List[int], List[Union[List[int],
              #  int]], int]` has no attribute `__iter__`.
              "".join([chr(chars[c % len(chars)]) for c in t])
              for t in text_lists
          ]
      except Exception:
          raise IndexError("Invalid text indices specified")
  
      draw = ImageDraw.Draw(image)
      for i, text_str in enumerate(text_strs):
          draw.text(
              xy=(x_pos * width, y_pos * height + i * (font_size + 5)),
              text=text_str,
              fill=(color[0], color[1], color[2], round(opacity * 255)),
              # pyre-fixme[6]: Expected `Optional[ImageFont._Font]` for 4th param but got
              #  `FreeTypeFont`.
              font=font,
          )
  
      imutils.get_metadata(
          metadata=metadata,
          function_name="overlay_text",
          aug_image=image,
          **func_kwargs,
      )
  
      return imutils.ret_and_save_image(image, output_path, src_mode)
  
imports: import io
  import math
  import os
  import pickle
  from copy import deepcopy
  from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  import numpy
  from augly import utils
  from augly.image import utils
  from augly.image.utils.bboxes import spatial_bbox_helper
  from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont
label_desc: Use image node_id 22 for calling a global function with example usage: overlay_text(image, output_path, text, font_file, font_size, opacity, color, x_pos, y_pos, metadata, bboxes, bbox_format) and returns: imutils