Skip to content

find_images_in_folder

anomaly_match.data_io.find_images_in_folder

Functions to retrieve image filenames from folders.

get_image_names_from_folder(folder_path, recursive=False, extensions=None)

Get all image filenames from a folder.

Parameters:

Name Type Description Default
folder_path str

Path to the folder containing images

required
recursive bool

Whether to search recursively in subfolders. Defaults to False.

False
extensions list[str] | None

List of file extensions to include. Defaults to SUPPORTED_IMAGE_EXTENSIONS if None.

None

Returns:

Name Type Description
list list[str]

List of image filenames (relative to folder_path)

Source code in anomaly_match/data_io/find_images_in_folder.py
def get_image_names_from_folder(
    folder_path: str, recursive: bool = False, extensions: list[str] | None = None
) -> list[str]:
    """Get all image filenames from a folder.

    Args:
        folder_path: Path to the folder containing images
        recursive: Whether to search recursively in subfolders. Defaults to False.
        extensions: List of file extensions to include.
                    Defaults to SUPPORTED_IMAGE_EXTENSIONS if None.

    Returns:
        list: List of image filenames (relative to folder_path)
    """
    logger.debug(f"Getting image names from folder: {folder_path}")

    # Use the default extensions if None is provided
    if extensions is None:
        extensions = SUPPORTED_IMAGE_EXTENSIONS

    image_filenames = []

    if recursive:
        # Use pathlib for recursive search
        for ext in extensions:
            # Search for both lowercase and uppercase extensions
            image_filenames.extend(
                [
                    os.path.relpath(str(path), folder_path)
                    for path in Path(folder_path).glob(f"**/*{ext}")
                ]
            )
            image_filenames.extend(
                [
                    os.path.relpath(str(path), folder_path)
                    for path in Path(folder_path).glob(f"**/*{ext.upper()}")
                ]
            )
        # On case-insensitive filesystems (Windows/macOS default), the lower-
        # and upper-case glob patterns match the same files, producing dupes.
        image_filenames = list(dict.fromkeys(image_filenames))
    else:
        # Use os.listdir for non-recursive search
        for f in os.listdir(folder_path):
            if any(f.lower().endswith(ext.lower()) for ext in extensions):
                image_filenames.append(f)

    logger.debug(f"Found {len(image_filenames)} images in {folder_path}")
    return image_filenames

get_image_paths_from_folder(folder_path, recursive=False, extensions=None)

Get all image paths from a folder.

Parameters:

Name Type Description Default
folder_path str

Path to the folder containing images

required
recursive bool

Whether to search recursively in subfolders. Defaults to False.

False
extensions list[str] | None

List of file extensions to include. Defaults to SUPPORTED_IMAGE_EXTENSIONS if None.

None

Returns:

Name Type Description
list list[str]

List of absolute image paths

Source code in anomaly_match/data_io/find_images_in_folder.py
def get_image_paths_from_folder(
    folder_path: str, recursive: bool = False, extensions: list[str] | None = None
) -> list[str]:
    """Get all image paths from a folder.

    Args:
        folder_path: Path to the folder containing images
        recursive: Whether to search recursively in subfolders. Defaults to False.
        extensions: List of file extensions to include.
                    Defaults to SUPPORTED_IMAGE_EXTENSIONS if None.

    Returns:
        list: List of absolute image paths
    """
    filenames = get_image_names_from_folder(folder_path, recursive, extensions)
    return [os.path.join(folder_path, filename) for filename in filenames]