def resize_and_stretch(
    img: Union[np.ndarray, Image.Image],
    target_size: Tuple[int, int],
    is_mask: bool = False,
    white_back: bool = False,
) -> np.ndarray:
    """
    Crop the image or mask, resize it to the target size, and stretch it.

    Args:
        img (Union[np.ndarray, Image.Image]): The input image or mask.
        target_size (Tuple[int, int]): The desired target size (width, height).
        is_mask (bool, optional): Whether the input is a mask (default is False).
        white_back (bool, optional): Whether to use white background (default is False).

    Returns:
        np.ndarray: The resized and stretched image as a NumPy array.
    """
    if isinstance(img, np.ndarray):
        img = Image.fromarray(img)

    # Calculate the aspect ratio
    width, height = img.size
    aspect_ratio = width / height

    # Calculate the new size while preserving the aspect ratio
    new_width = int(min(target_size[0], target_size[1] * aspect_ratio))
    new_height = int(min(target_size[1], target_size[0] / aspect_ratio))

    img = img.resize((new_width, new_height))

    if is_mask:
        resized_img = Image.new("L", target_size, color="white" if white_back else "black")
    else:
        resized_img = Image.new("RGB", target_size, color="white" if white_back else "black")

    # Add to the center
    offset_x = (target_size[0] - img.size[0]) // 2
    offset_y = (target_size[1] - img.size[1]) // 2

    # Paste the image
    resized_img.paste(img, (offset_x, offset_y))
    resized_img = np.array(resized_img)
    return resized_img

    Crop the image or mask, resize it to the target size, and stretch it.

    Args:
        img (Union[np.ndarray, Image.Image]): The input image or mask.
        target_size (Tuple[int, int]): The desired target size (width, height).
        is_mask (bool, optional): Whether the input is a mask (default is False).
        white_back (bool, optional): Whether to use white background (default is False).

    Returns:
        np.ndarray: The resized and stretched image as a NumPy array.
    
scripts/easyphoto_utils/tryon_utils.py