def duplicate_filename(path: str | Path) -> Path:
    """Add numeric suffix to filename if it already exists.

    Args:
        path (str | Path): Path to file

    Returns:
        Path: Path with numeric suffix if original exists

    Examples:
        >>> duplicate_filename("image.jpg")  # File doesn't exist
        PosixPath('image.jpg')

        >>> duplicate_filename("exists.jpg")  # File exists
        PosixPath('exists_1.jpg')

        >>> duplicate_filename("exists.jpg")  # Both exist
        PosixPath('exists_2.jpg')
    """
    path = Path(path)

    if not path.exists():
        return path

    i = 0
    while True:
        duplicated_path = path if i == 0 else path.parent / (path.stem + f"_{i}" + path.suffix)
        if not duplicated_path.exists():
            break
        i += 1

    return duplicated_path
Add numeric suffix to filename if it already exists.

    Args:
        path (str | Path): Path to file

    Returns:
        Path: Path with numeric suffix if original exists

    Examples:
        >>> duplicate_filename("image.jpg")  # File doesn't exist
        PosixPath('image.jpg')

        >>> duplicate_filename("exists.jpg")  # File exists
        PosixPath('exists_1.jpg')

        >>> duplicate_filename("exists.jpg")  # Both exist
        PosixPath('exists_2.jpg')
    
src/anomalib/data/utils/image.py