Browse Source

color coded rois are labeled

master
Holger Frey 6 years ago
parent
commit
b9569b117f
  1. 3
      mtor/__init__.py
  2. 48
      mtor/postproc.py
  3. 7
      mtor/workflows.py
  4. 4
      test_mtor.py

3
mtor/__init__.py

@ -20,4 +20,5 @@ def run():
def run_data(): def run_data():
cached_data_workflow("mtor-bilder") dw = cached_data_workflow("mtor-bilder") # noqa: F841
# postprocessing_workflow(dw.data, dw.parameters) # noqa: F841

48
mtor/postproc.py

@ -1,6 +1,9 @@
import pathlib 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): def stem_file_list(selected_files):
@ -8,20 +11,38 @@ def stem_file_list(selected_files):
def rename_color_coded_images(file_stems, parameters): def rename_color_coded_images(file_stems, parameters):
rename_pairs = [] colors = {LABEL_DISCARDED: (198, 78, 82), LABEL_SELECTED: (74, 114, 174)}
for path in parameters.colored_dir.iterdir(): 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("."): if not path.stem.startswith("."):
label = ( label = (
LABEL_SELECTED if path.stem in file_stems else LABEL_DISCARDED LABEL_SELECTED if path.stem in file_stems else LABEL_DISCARDED
) )
new_name = path.with_name(f"{path.stem}_{label}{path.suffix}") new_path = path.with_name(f"{path.stem}_{label}{path.suffix}")
rename_pairs.append((path, new_name)) img = Image.open(path)
return rename_pairs # 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): def sort_cut_images(file_stems, parameters):
sort_pairs = [] for path in tqdm(list(parameters.cuts_dir.iterdir())):
for path in parameters.cuts_dir.iterdir():
if not path.stem.startswith("."): if not path.stem.startswith("."):
label = ( label = (
LABEL_SELECTED if path.stem in file_stems else LABEL_DISCARDED 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"] parameters[f"cuts_{label}_dir"]
/ f"{path.stem}_cut_{label}{path.suffix}" / f"{path.stem}_cut_{label}{path.suffix}"
) )
sort_pairs.append((path, new_path)) path.rename(new_path)
return sort_pairs
def remove_cuts_dir(parameters): def remove_cuts_dir(parameters):
for item in parameters["cuts_dir"].iterdir(): cuts_dir = parameters["cuts_dir"]
item.unlink() if cuts_dir.is_dir():
parameters["cuts_dir"].rmdir() for item in cuts_dir.iterdir():
item.unlink()
parameters["cuts_dir"].rmdir()

7
mtor/workflows.py

@ -79,11 +79,8 @@ def data_workflow(stats_results, parameters):
def postprocessing_workflow(selected_files, parameters): def postprocessing_workflow(selected_files, parameters):
print("4/4: Post processing") print("4/4: Post processing")
file_stems = stem_file_list(selected_files) file_stems = stem_file_list(selected_files)
cc_rename_pairs = rename_color_coded_images(file_stems, parameters) rename_color_coded_images(file_stems, parameters)
cut_sort_pairs = sort_cut_images(file_stems, parameters) 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)
remove_cuts_dir(parameters) remove_cuts_dir(parameters)

4
test_mtor.py

@ -5,5 +5,5 @@ tif_dir = "original_tifs"
#mtor.process_tifs(tif_dir, 50, 910, 300, 660, boost=5) #mtor.process_tifs(tif_dir, 50, 910, 300, 660, boost=5)
#mtor.run() mtor.run()
mtor.run_data() #mtor.run_data()

Loading…
Cancel
Save