| | import os |
| | def check_exists_files(files,dirs,exit_on_error=True): |
| | if files is not None: |
| | if isinstance(files, str): |
| | files = [files] |
| | for file in files: |
| | if not os.path.isfile(file): |
| | print(f"File {file} not found") |
| | if exit_on_error: |
| | exit(1) |
| | else: |
| | return 1 |
| | if dirs is not None: |
| | if isinstance(dirs, str): |
| | dirs = [dirs] |
| | for dir in dirs: |
| | if not os.path.isdir(dir): |
| | print(f"Dir {dir} not found") |
| | if exit_on_error: |
| | exit(1) |
| | else: |
| | return 1 |
| | return 0 |
| |
|
| | image_extensions =[".jpg"] |
| |
|
| | def add_name_suffix(file_name,suffix,replace_suffix=False): |
| | if not suffix.startswith("_"): |
| | suffix="_"+suffix |
| |
|
| | name,ext = os.path.splitext(file_name) |
| | if replace_suffix: |
| | index = name.rfind("_") |
| | if index!=-1: |
| | return f"{name[0:index]}{suffix}{ext}" |
| | |
| | return f"{name}{suffix}{ext}" |
| |
|
| | def replace_extension(file_name,new_extension,suffix=None,replace_suffix=False): |
| | if not new_extension.startswith("."): |
| | new_extension="."+new_extension |
| |
|
| | name,ext = os.path.splitext(file_name) |
| | new_file = f"{name}{new_extension}" |
| | if suffix: |
| | return add_name_suffix(name+new_extension,suffix,replace_suffix) |
| | return new_file |
| |
|
| | def list_digit_images(input_dir,sort=True): |
| | digit_images = [] |
| | global image_extensions |
| | files = os.listdir(input_dir) |
| | for file in files: |
| | if file.endswith(".jpg"): |
| | base,ext = os.path.splitext(file) |
| | if not base.isdigit(): |
| | continue |
| | digit_images.append(file) |
| |
|
| | if sort: |
| | digit_images.sort() |
| |
|
| | return digit_images |
| | def list_suffix_images(input_dir,suffix,is_digit=True,sort=True): |
| | digit_images = [] |
| | global image_extensions |
| | files = os.listdir(input_dir) |
| | for file in files: |
| | if file.endswith(".jpg"): |
| | base,ext = os.path.splitext(file) |
| | if base.endswith(suffix): |
| | if is_digit: |
| | if not base.replace(suffix,"").isdigit(): |
| | continue |
| | digit_images.append(file) |
| |
|
| | if sort: |
| | digit_images.sort() |
| |
|
| | return digit_images |
| |
|
| | import time |
| |
|
| | class ProgressTracker: |
| | """ |
| | 処理の進捗状況を追跡し、経過時間と残り時間を表示するクラス。 |
| | """ |
| |
|
| | def __init__(self,key, total_target): |
| | """ |
| | コンストラクタ |
| | |
| | Args: |
| | total_target (int): 処理対象の総数 |
| | """ |
| | self.key = key |
| | self.total_target = total_target |
| | self.complete_target = 0 |
| | self.start_time = time.time() |
| |
|
| | def update(self): |
| | """ |
| | 進捗を1つ進める。 |
| | 経過時間と残り時間を表示する。 |
| | """ |
| | self.complete_target += 1 |
| | current_time = time.time() |
| | consumed_time = current_time - self.start_time |
| | remain_time = (consumed_time / self.complete_target) * (self.total_target - self.complete_target) if self.complete_target > 0 else 0 |
| | print(f"stepped {self.key} {self.total_target} of {self.complete_target}, consumed {(consumed_time / 60):.1f} min, remain {(remain_time / 60):.1f} min") |
| |
|
| | |