Skip to content

getImages

Short Description

The pp.getImages function retrieves image file paths from a specified directory, with an optional filter for file extensions. It simplifies collecting images for analysis, allowing for specific format selection in folders with multiple elements.

Function

getImages(folderPath, extension=None)

Parameters:

Name Type Description Default
folderPath str

The path to the directory from which files will be listed. This directory should already exist and be accessible at the time of function invocation.

required
extension str

The file extension used to filter the files listed by the function. If specified, the function will only return files that end with the given extension (e.g., 'tif' ). The extension should not include the leading period ('.'). If None or not provided, all files in the directory will be listed. Default is None.

None

Returns:

Name Type Description
list list

A list containing the full paths to the files that meet the criteria. If an extension is specified, only files with that extension will be included in the list.

Example
# Listing all `tif` files in a directory called 'documents':

imagePaths = pp.getImages(directory='/path/to/dir/documents', extension = 'tif')
Source code in pixelpartitioner/getImages.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def getImages (folderPath, 
               extension=None):

    """
Parameters:
    folderPath (str):
        The path to the directory from which files will be listed. This directory should already exist and be accessible at the time of function invocation.

    extension (str, optional):
        The file extension used to filter the files listed by the function. If specified, the function will only return files that end with the given extension (e.g., 'tif' ). The extension should not include the leading period ('.'). If None or not provided, all files in the directory will be listed. Default is None.

Returns:
    list (list): 
        A list containing the full paths to the files that meet the criteria. If an extension is specified, only files with that extension will be included in the list.


Example:
    ```python

    # Listing all `tif` files in a directory called 'documents':

    imagePaths = pp.getImages(directory='/path/to/dir/documents', extension = 'tif')

    ```


"""

    files = []  # List to store the paths of files
    # Normalize the extension to ensure it starts with a dot
    if extension and not extension.startswith('.'):
        extension = '.' + extension

    for file in os.listdir(folderPath):
        file_path = os.path.join(folderPath, file)
        # Check if it's a file and not a directory
        if os.path.isfile(file_path):
            # If extension is provided, filter files by the extension
            if extension:
                if file.endswith(extension):
                    files.append(file_path)
            else:
                files.append(file_path)

    return files