This repository has been archived by the owner on May 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheet_dissection.py
executable file
·100 lines (71 loc) · 2.95 KB
/
sheet_dissection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
"""Save the allometry sheet dissection for inspection."""
import argparse
import logging
import textwrap
from os import makedirs
from pathlib import Path
from PIL import ImageDraw
from allometry.allometry_sheet import AllometrySheet
from allometry.util import finished, started
BOX_COLOR = (0, 255, 255)
def dissect(args):
"""Dissect the sheets."""
make_dirs(args)
image_paths = sorted(args.sheet_dir.glob(f'*.{args.image_suffix}'))
if args.filter:
image_paths = [p for p in image_paths if str(p).find(args.filter) > -1]
for image_path in image_paths:
logging.info(f'{image_path}')
sheet = AllometrySheet(image_path, args.rotate)
dissected = sheet.binary.convert('RGB')
draw = ImageDraw.Draw(dissected)
for i in range(len(sheet)):
_, box = sheet[i]
box = box.tolist()
draw.rectangle(box, outline=BOX_COLOR)
if args.show_rows:
width = sheet.binary.size[0]
for top, bottom in sheet.rows:
draw.line((0, top, width, top), width=1, fill='red')
draw.line((0, bottom, width, bottom), width=1, fill='yellow')
dissect_path = args.dissection_dir / (image_path.stem + '.jpg')
dissected.save(dissect_path, 'JPEG')
def make_dirs(args):
"""Create output directories."""
if args.dissection_dir:
makedirs(args.dissection_dir, exist_ok=True)
def parse_args():
"""Process command-line arguments."""
description = """Save the results of image dissection."""
arg_parser = argparse.ArgumentParser(
description=textwrap.dedent(description),
fromfile_prefix_chars='@')
arg_parser.add_argument(
'--sheet-dir', required=True, type=Path,
help="""The directory containing the images of the allometry sheets to convert.
They should all have the same orientation.""")
arg_parser.add_argument(
'--image-suffix', default='tif', help="""The suffix for the images.""")
arg_parser.add_argument(
'--rotate', type=int, default=0,
help="""Rotate all images counterclockwise (in degrees) to orient
them. Typically, you will use 0, 90, 180, or 270 degrees. Note that this
means that all of the images in the --image-dir should all have the same
original orientation. This is only to get image oriented properly, the
program will fine tune the rotation (deskew) later.
(default: %(default)s)""")
arg_parser.add_argument(
'--dissection-dir', required=True, type=Path,
help="""Where to put the dissected images.""")
arg_parser.add_argument(
'--filter', help="""Filter images.""")
arg_parser.add_argument(
'--show-rows', action='store_true', help="""Show rows.""")
args = arg_parser.parse_args()
return args
if __name__ == '__main__':
started()
ARGS = parse_args()
dissect(ARGS)
finished()