|
|
@ -1,3 +1,5 @@ |
|
|
|
|
|
|
|
import functools |
|
|
|
|
|
|
|
import multiprocessing |
|
|
|
import pathlib |
|
|
|
import pathlib |
|
|
|
|
|
|
|
|
|
|
|
from PIL import Image, ImageDraw, ImageFont |
|
|
|
from PIL import Image, ImageDraw, ImageFont |
|
|
@ -12,28 +14,44 @@ def stem_file_list(selected_files): |
|
|
|
|
|
|
|
|
|
|
|
def annotate_color_coded_images(file_stems, parameters): |
|
|
|
def annotate_color_coded_images(file_stems, parameters): |
|
|
|
colors = {LABEL_DISCARDED: (198, 78, 82), LABEL_SELECTED: (74, 114, 174)} |
|
|
|
colors = {LABEL_DISCARDED: (198, 78, 82), LABEL_SELECTED: (74, 114, 174)} |
|
|
|
top = parameters.roi_top |
|
|
|
roi_rectangle = [ |
|
|
|
bottom = parameters.roi_bottom |
|
|
|
(parameters.roi_left, parameters.roi_top), |
|
|
|
left = parameters.roi_left |
|
|
|
(parameters.roi_right, parameters.roi_bottom), |
|
|
|
right = parameters.roi_right |
|
|
|
] |
|
|
|
|
|
|
|
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: |
|
|
|
try: |
|
|
|
font = ImageFont.truetype("Courier New.ttf", 18) |
|
|
|
font = ImageFont.truetype("Courier New.ttf", 18) |
|
|
|
except OSError: |
|
|
|
except OSError: |
|
|
|
font = None |
|
|
|
font = None |
|
|
|
font_y_pos = parameters.image_height - 25 - 18 |
|
|
|
label = LABEL_SELECTED if path.stem in file_stems else LABEL_DISCARDED |
|
|
|
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}") |
|
|
|
new_path = path.with_name(f"{path.stem}_{label}{path.suffix}") |
|
|
|
img = Image.open(path) |
|
|
|
img = Image.open(path) |
|
|
|
# draw a colored roi |
|
|
|
# draw a colored roi |
|
|
|
draw = ImageDraw.Draw(img) |
|
|
|
draw = ImageDraw.Draw(img) |
|
|
|
color = colors[label] |
|
|
|
color = colors[label] |
|
|
|
draw.rectangle( |
|
|
|
draw.rectangle(roi_rectangle, outline=color, width=2) |
|
|
|
[(left, top), (right, bottom)], outline=color, width=2 |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
# add sequence number |
|
|
|
# add sequence number |
|
|
|
sequence_nr = RE_DIGITS.search(path.name).group() |
|
|
|
sequence_nr = RE_DIGITS.search(path.name).group() |
|
|
|
draw.text((25, font_y_pos), sequence_nr, font=font) |
|
|
|
draw.text((25, font_y_pos), sequence_nr, font=font) |
|
|
|