def calc_pct_inside_image(fg, bg, x_coord_start, y_coord_start):
  """Calculate the percentage of the object that is inside the image.

  This calculation is based on the bounding box of the object
  as opposed to object pixels.

  Args:
    fg: PIL image. Foreground image.
    bg: PIL image. Background image.
    x_coord_start: leftmost x-coordinate of foreground object.
    y_coord_start: topmost y-coordinate of foreground object.

  Returns:
    Float between 0.0 and 1.0 inclusive, indicating the percentage of the
    object that is in the image.
  """
  x_coord_end = x_coord_start + fg.width
  y_coord_end = y_coord_start + fg.height

  x_obj_start = max(x_coord_start, 0)
  x_obj_end = min(x_coord_end, bg.width)
  y_obj_start = max(y_coord_start, 0)
  y_obj_end = min(y_coord_end, bg.height)

  object_area = fg.width * fg.height
  area_inside_image = (x_obj_end - x_obj_start) * (y_obj_end - y_obj_start)
  pct_inside_image = area_inside_image / object_area
  return pct_inside_image