Browse Source

annotating images uses multiprocessing

master
Holger Frey 6 years ago
parent
commit
51a6ff6115
  1. 3
      ideas.md
  2. 1
      mtor/imageproc.py
  3. 64
      mtor/postproc.py
  4. 0
      mtor/report.py

3
ideas.md

@ -1,3 +0,0 @@
Further Ideas:
- automatic use of cached values

1
mtor/imageproc.py

@ -23,7 +23,6 @@ def scale_and_colorize(tif_array, parameters):
adjusted_array = (tif_array.data - parameters.offset) // parameters.scale adjusted_array = (tif_array.data - parameters.offset) // parameters.scale
# paint roi # paint roi
top = parameters.roi_top top = parameters.roi_top
bottom = parameters.roi_bottom bottom = parameters.roi_bottom
right = parameters.roi_right right = parameters.roi_right

64
mtor/postproc.py

@ -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,33 +14,49 @@ 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())): new_path = path.with_name(f"{path.stem}_{label}{path.suffix}")
if not path.stem.startswith("."): img = Image.open(path)
label = ( # draw a colored roi
LABEL_SELECTED if path.stem in file_stems else LABEL_DISCARDED draw = ImageDraw.Draw(img)
) color = colors[label]
new_path = path.with_name(f"{path.stem}_{label}{path.suffix}") draw.rectangle(roi_rectangle, outline=color, width=2)
img = Image.open(path) # add sequence number
# draw a colored roi sequence_nr = RE_DIGITS.search(path.name).group()
draw = ImageDraw.Draw(img) draw.text((25, font_y_pos), sequence_nr, font=font)
color = colors[label] img.save(new_path)
draw.rectangle( path.unlink()
[(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):

0
mtor/report.py

Loading…
Cancel
Save