diff --git a/mtor/__init__.py b/mtor/__init__.py index 5587d5c..b56c1c6 100644 --- a/mtor/__init__.py +++ b/mtor/__init__.py @@ -20,4 +20,5 @@ def run(): def run_data(): - cached_data_workflow("mtor-bilder") + dw = cached_data_workflow("mtor-bilder") # noqa: F841 + # postprocessing_workflow(dw.data, dw.parameters) # noqa: F841 diff --git a/mtor/postproc.py b/mtor/postproc.py index d704915..bdcf64e 100644 --- a/mtor/postproc.py +++ b/mtor/postproc.py @@ -1,6 +1,9 @@ import pathlib -from .commons import LABEL_SELECTED, LABEL_DISCARDED +from PIL import Image, ImageDraw, ImageFont +from tqdm import tqdm + +from .commons import RE_DIGITS, LABEL_SELECTED, LABEL_DISCARDED def stem_file_list(selected_files): @@ -8,20 +11,38 @@ def stem_file_list(selected_files): def rename_color_coded_images(file_stems, parameters): - rename_pairs = [] - for path in parameters.colored_dir.iterdir(): + colors = {LABEL_DISCARDED: (198, 78, 82), LABEL_SELECTED: (74, 114, 174)} + top = parameters.roi_top + bottom = parameters.roi_bottom + left = parameters.roi_left + right = parameters.roi_right + try: + font = ImageFont.truetype("Courier New.ttf", 18) + except OSError: + font = None + font_y_pos = parameters.image_height - 25 - 18 + for path in tqdm(list(parameters.colored_dir.iterdir())): if not path.stem.startswith("."): label = ( LABEL_SELECTED if path.stem in file_stems else LABEL_DISCARDED ) - new_name = path.with_name(f"{path.stem}_{label}{path.suffix}") - rename_pairs.append((path, new_name)) - return rename_pairs + new_path = path.with_name(f"{path.stem}_{label}{path.suffix}") + img = Image.open(path) + # draw a colored roi + draw = ImageDraw.Draw(img) + color = colors[label] + draw.rectangle( + [(left, top), (right, bottom)], outline=color, width=2 + ) + # add sequence number + sequence_nr = RE_DIGITS.search(path.name).group() + draw.text((25, font_y_pos), sequence_nr, font=font) + img.save(new_path) + path.unlink() def sort_cut_images(file_stems, parameters): - sort_pairs = [] - for path in parameters.cuts_dir.iterdir(): + for path in tqdm(list(parameters.cuts_dir.iterdir())): if not path.stem.startswith("."): label = ( LABEL_SELECTED if path.stem in file_stems else LABEL_DISCARDED @@ -30,11 +51,12 @@ def sort_cut_images(file_stems, parameters): parameters[f"cuts_{label}_dir"] / f"{path.stem}_cut_{label}{path.suffix}" ) - sort_pairs.append((path, new_path)) - return sort_pairs + path.rename(new_path) def remove_cuts_dir(parameters): - for item in parameters["cuts_dir"].iterdir(): - item.unlink() - parameters["cuts_dir"].rmdir() + cuts_dir = parameters["cuts_dir"] + if cuts_dir.is_dir(): + for item in cuts_dir.iterdir(): + item.unlink() + parameters["cuts_dir"].rmdir() diff --git a/mtor/workflows.py b/mtor/workflows.py index d024c5a..b9a7ec7 100644 --- a/mtor/workflows.py +++ b/mtor/workflows.py @@ -79,11 +79,8 @@ def data_workflow(stats_results, parameters): def postprocessing_workflow(selected_files, parameters): print("4/4: Post processing") file_stems = stem_file_list(selected_files) - cc_rename_pairs = rename_color_coded_images(file_stems, parameters) - cut_sort_pairs = sort_cut_images(file_stems, parameters) - file_pairs = cc_rename_pairs + cut_sort_pairs - for old_path, new_path in tqdm(file_pairs): - old_path.rename(new_path) + rename_color_coded_images(file_stems, parameters) + sort_cut_images(file_stems, parameters) remove_cuts_dir(parameters) diff --git a/test_mtor.py b/test_mtor.py index cef63aa..459d471 100644 --- a/test_mtor.py +++ b/test_mtor.py @@ -5,5 +5,5 @@ tif_dir = "original_tifs" #mtor.process_tifs(tif_dir, 50, 910, 300, 660, boost=5) -#mtor.run() -mtor.run_data() +mtor.run() +#mtor.run_data()