|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+from django.core.management.base import BaseCommand
|
|
|
2
|
+from ...xword import extract_crossword
|
|
|
3
|
+
|
|
|
4
|
+
|
|
|
5
|
+class Command(BaseCommand):
|
|
|
6
|
+ help = "Extracts a clean crossword image from a photograph."
|
|
|
7
|
+
|
|
|
8
|
+ def add_arguments(self, parser):
|
|
|
9
|
+ parser.add_argument('input_file_name')
|
|
|
10
|
+ parser.add_argument('output_file_name')
|
|
|
11
|
+
|
|
|
12
|
+ parser.add_argument('--filter-colours', action='store_true')
|
|
|
13
|
+ parser.add_argument('--colour-filter-threshold', type=int, default=48)
|
|
|
14
|
+
|
|
|
15
|
+ parser.add_argument('--gaussian-blur-size', type=int, default=11)
|
|
|
16
|
+ parser.add_argument('--adaptive-threshold-block-size', type=int, default=11)
|
|
|
17
|
+ parser.add_argument('--adaptive-threshold-mean-adjustment', type=int, default=2)
|
|
|
18
|
+
|
|
|
19
|
+ parser.add_argument('--not-square', action='store_true')
|
|
|
20
|
+
|
|
|
21
|
+ parser.add_argument('--num-dilations', type=int, default=1)
|
|
|
22
|
+
|
|
|
23
|
+ parser.add_argument('--contour-erosion-kernel-size', type=int, default=5)
|
|
|
24
|
+ parser.add_argument('--contour-erosion-iterations', type=int, default=5)
|
|
|
25
|
+ parser.add_argument('--line-detector-element-size', type=int, default=51)
|
|
|
26
|
+
|
|
|
27
|
+ parser.add_argument('--sampling-block-size-ratio', type=float, default=0.25)
|
|
|
28
|
+ parser.add_argument('--sampling-threshold-quantile', type=float, default=0.3)
|
|
|
29
|
+ parser.add_argument('--sampling-threshold', type=int)
|
|
|
30
|
+
|
|
|
31
|
+ parser.add_argument('--grid-line-thickness', type=int, default=4)
|
|
|
32
|
+ parser.add_argument('--grid-square-size', type=int, default=64)
|
|
|
33
|
+ parser.add_argument('--grid-border-size', type=int, default=20)
|
|
|
34
|
+
|
|
|
35
|
+ group = parser.add_mutually_exclusive_group()
|
|
|
36
|
+ group.add_argument('--out')
|
|
|
37
|
+ group.add_argument('--html')
|
|
|
38
|
+
|
|
|
39
|
+ def handle(self, *args, **options):
|
|
|
40
|
+ image, warnings = extract_crossword(
|
|
|
41
|
+ options['input_file_name'],
|
|
|
42
|
+ gaussian_blur_size=options['gaussian_blur_size'],
|
|
|
43
|
+ adaptive_threshold_block_size=options['adaptive_threshold_block_size'],
|
|
|
44
|
+ adaptive_threshold_mean_adjustment=options['adaptive_threshold_mean_adjustment'],
|
|
|
45
|
+ square=not options['not_square'],
|
|
|
46
|
+ num_dilations=options['num_dilations'],
|
|
|
47
|
+ contour_erosion_kernel_size=options['contour_erosion_kernel_size'],
|
|
|
48
|
+ contour_erosion_iterations=options['contour_erosion_iterations'],
|
|
|
49
|
+ line_detector_element_size=options['line_detector_element_size'],
|
|
|
50
|
+ sampling_block_size_ratio=options['sampling_block_size_ratio'],
|
|
|
51
|
+ sampling_threshold_quantile=options['sampling_threshold_quantile'],
|
|
|
52
|
+ sampling_threshold=options['sampling_threshold'],
|
|
|
53
|
+ grid_line_thickness=options['grid_line_thickness'],
|
|
|
54
|
+ grid_square_size=options['grid_square_size'],
|
|
|
55
|
+ grid_border_size=options['grid_border_size']
|
|
|
56
|
+ )
|
|
|
57
|
+ for warning in warnings:
|
|
|
58
|
+ print('WARNING: ' + warning)
|
|
|
59
|
+ with open(options['output_file_name'], 'wb') as f:
|
|
|
60
|
+ f.write(image)
|