|
|
@ -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"] |
|
|
|
|
|
|
|
if cuts_dir.is_dir(): |
|
|
|
|
|
|
|
for item in cuts_dir.iterdir(): |
|
|
|
item.unlink() |
|
|
|
item.unlink() |
|
|
|
parameters["cuts_dir"].rmdir() |
|
|
|
parameters["cuts_dir"].rmdir() |
|
|
|