|
|
|
@ -1,3 +1,5 @@
@@ -1,3 +1,5 @@
|
|
|
|
|
import functools |
|
|
|
|
import multiprocessing |
|
|
|
|
import pathlib |
|
|
|
|
|
|
|
|
|
from PIL import Image, ImageDraw, ImageFont |
|
|
|
@ -12,28 +14,44 @@ def stem_file_list(selected_files):
@@ -12,28 +14,44 @@ 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 |
|
|
|
|
) |
|
|
|
|
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 |
|
|
|
|
) |
|
|
|
|
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) |
|
|
|
|