From 51a6ff611598618b2a66a4d691c1b6dbd75ba571 Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Mon, 29 Apr 2019 11:44:38 +0200 Subject: [PATCH] annotating images uses multiprocessing --- ideas.md | 3 --- mtor/imageproc.py | 1 - mtor/postproc.py | 64 ++++++++++++++++++++++++++++++----------------- mtor/report.py | 0 4 files changed, 41 insertions(+), 27 deletions(-) delete mode 100644 ideas.md create mode 100644 mtor/report.py diff --git a/ideas.md b/ideas.md deleted file mode 100644 index d92f820..0000000 --- a/ideas.md +++ /dev/null @@ -1,3 +0,0 @@ -Further Ideas: - -- automatic use of cached values diff --git a/mtor/imageproc.py b/mtor/imageproc.py index 361147e..2291f3e 100644 --- a/mtor/imageproc.py +++ b/mtor/imageproc.py @@ -23,7 +23,6 @@ def scale_and_colorize(tif_array, parameters): adjusted_array = (tif_array.data - parameters.offset) // parameters.scale # paint roi - top = parameters.roi_top bottom = parameters.roi_bottom right = parameters.roi_right diff --git a/mtor/postproc.py b/mtor/postproc.py index 99784af..a98d37e 100644 --- a/mtor/postproc.py +++ b/mtor/postproc.py @@ -1,3 +1,5 @@ +import functools +import multiprocessing import pathlib from PIL import Image, ImageDraw, ImageFont @@ -12,33 +14,49 @@ def stem_file_list(selected_files): def annotate_color_coded_images(file_stems, parameters): 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 + roi_rectangle = [ + (parameters.roi_left, parameters.roi_top), + (parameters.roi_right, parameters.roi_bottom), + ] + font_y_pos = parameters.image_height - 25 - 18 + color_coded_images = [ + p + for p in parameters.colored_dir.iterdir() + if not p.name.startswith(".") + ] + func = functools.partial( + annotate_one_color_coded_image, + file_stems=file_stems, + roi_rectangle=roi_rectangle, + colors=colors, + font_y_pos=font_y_pos, + ) + cpu_count = multiprocessing.cpu_count() + total = len(color_coded_images) + with multiprocessing.Pool(cpu_count) as mpp: + list(tqdm(mpp.imap(func, color_coded_images), total=total)) + + +def annotate_one_color_coded_image( + path, file_stems, roi_rectangle, colors, font_y_pos +): + # fonts can't be pickled, we need to open it every time. 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_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() + label = LABEL_SELECTED if path.stem in file_stems else LABEL_DISCARDED + 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(roi_rectangle, 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): diff --git a/mtor/report.py b/mtor/report.py new file mode 100644 index 0000000..e69de29