Team Ai
Apppublic

Dynamatrix/DiffBIR-OpenXLab

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
file.py44 linesDownload Raw Back to utils
1import os2from typing import List, Tuple3 4 5def load_file_list(file_list_path: str) -> List[str]:6    files = []7    # each line in file list contains a path of an image8    with open(file_list_path, "r") as fin:9        for line in fin:10            path = line.strip()11            if path:12                files.append(path)13    return files14 15 16def list_image_files(17    img_dir: str,18    exts: Tuple[str]=(".jpg", ".png", ".jpeg"),19    follow_links: bool=False,20    log_progress: bool=False,21    log_every_n_files: int=10000,22    max_size: int=-123) -> List[str]:24    files = []25    for dir_path, _, file_names in os.walk(img_dir, followlinks=follow_links):26        early_stop = False27        for file_name in file_names:28            if os.path.splitext(file_name)[1].lower() in exts:29                if max_size >= 0 and len(files) >= max_size:30                    early_stop = True31                    break32                files.append(os.path.join(dir_path, file_name))33                if log_progress and len(files) % log_every_n_files == 0:34                    print(f"find {len(files)} images in {img_dir}")35        if early_stop:36            break37    return files38 39 40def get_file_name_parts(file_path: str) -> Tuple[str, str, str]:41    parent_path, file_name = os.path.split(file_path)42    stem, ext = os.path.splitext(file_name)43    return parent_path, stem, ext44