from PIL import Image


def crop_and_insert(
    image: Image.Image,
    x: int,
    y: int,
    h: int,
    w: int,
    bg_color: tuple[int, int, int] = (255, 255, 255),
) -> Image.Image:
    """
    Crops a region from an inverted grayscale image and inserts it into a white image of the same size as the original.

    Parameters:
    - fp: File path or a file object of the original image.
    - x, y: The top-left coordinates of the rectangle to be cropped.
    - h, w: The height and width of the rectangle to be cropped.

    Returns:
    A PIL Image object containing the original image with the specified region replaced.
    """
    # Load the original image
    original_image = image

    # Crop the specified region from the inverted image
    crop_rectangle = (x, y, x + w, y + h)
    cropped_content = original_image.crop(crop_rectangle)

    # Create a new white image of the same size as the original image
    white_background = Image.new("RGB", original_image.size, bg_color)

    # Paste the cropped content onto the white image at the specified location
    white_background.paste(cropped_content, (x, y))

    # The final image can be displayed or saved as needed
    return white_background
