-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.py
executable file
·154 lines (146 loc) · 4.85 KB
/
scan.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
import uuid
from configargparse import ArgParser
from src.exporter import Exporter
from src.preprocessor import Preprocessor
from src.scanner import Scanner
from src.utils import Utils
def main(config):
scanner = Scanner(config)
preprocessor = Preprocessor(config)
utils = Utils(config)
scanner.scan(front=True)
if config.manual_duplex:
print("rotate pages and press enter")
input()
scanner.scan(front=False)
pages = scanner.get_pages()
preprocessor.process(pages)
exporter = Exporter(config)
exporter.save_as_pdf(pages)
if utils.show_preview():
exporter.upload_doc()
utils.clean_up(pages)
def validate_config(config):
if config.manual_duplex and config.duplex:
raise ValueError("Canot use automatic duplex and manual duplex together!")
if __name__ == "__main__":
parser = ArgParser(
description="Scan from ADF, preprocess and upload do Docspell.",
default_config_files=["defaults.conf", "custom.conf"],
)
parser.add("--api_key", required=True, help="Docspell's API KEY")
parser.add(
"--docspell_url",
required=True,
help="Url to docspell, e.g. http://<docpsell_host>:7880/",
)
parser.add(
"-n",
"--name",
type=str,
default=uuid.uuid1(),
help="Name of the scan. Default is a random String",
)
parser.add("-d", "--duplex", action="store_true", help="Scan front and back pages")
parser.add(
"-m",
"--manual_duplex",
action="store_true",
help="Scan front pages using ADF. Manual rotate pages. "
"Scan back pages using ADF.",
)
parser.add(
"-f",
"--flatbed",
action="store_true",
help="Manually scan one page after the other from flatbed.",
)
parser.add("-c", "--color", action="store_true", help="Do a colored scan")
parser.add("--keep_scans", action="store_true", help="Do not delete raw scans")
parser.add("--keep_pdf", action="store_true", help="Do not delete combined pdf")
parser.add(
"--empty_threshold",
type=float,
help="Threshold to determine if a page is empty. "
"The emptier a document is, the smaller it' value becomes.",
)
parser.add(
"--device",
help="Device name (e.g. SCANNER_ABC). "
"Default: None (use systems default scanner)",
)
parser.add("--resolution", help="Scan resolution in dpi")
parser.add(
"--command_flatbed",
help="scanimage option to perform a flatbed scan "
"(it's depending on your scanner).",
)
parser.add(
"--command_adf",
help="scanimage option to perform a single-page adf scan "
"(it's depending on your scanner).",
)
parser.add(
"--command_duplex_adf",
help="scanimage option to perform a duplex adf scan "
"(it's depending on your scanner).",
)
parser.add("--preview", help="Show a preview before uploading to docspell")
parser.add(
"--skip_backside_rotation",
action="store_true",
help="In duplex mode backsides are rotated, so the scripts rotates them back. "
"Enable this to skip rotation.",
)
parser.add(
"--skip_trim_pages",
action="store_true",
help="By default the white border around pages will be removed. "
"Enable this to skip trimming pages.",
)
parser.add(
"--skip_empty_page_removal",
action="store_true",
help="By default empty pages are deleted. "
"Enable this to skip empty page deletion.",
)
parser.add(
"--skip_length_trimming",
action="store_true",
help="By default length of the scans is trimmed to DIN A4 ratio. "
"Enable this to skip trimming page lengths.",
)
parser.add(
"--start_count",
type=int,
help="Overwrite the first page number. "
"Useful if the scan was canceled and you want to resume a scan.",
)
parser.add(
"--scan_format",
type=str,
default="pnm",
help="Specifies the format in which to scan. "
"However, the format is only temporary, "
"scans are always converted to jpg and then bundled as pdf.",
)
parser.add(
"--black_level",
type=float,
default=0.0,
help="Lower level (0.0 - 1.0) from which points are colored black. "
"Can be increased to blacken elements that appear dark gray. "
"0.0 disables blackening.",
)
parser.add(
"--white_level",
type=float,
default=1.0,
help="Upper level (1.0 - 0.0) from which points are colored white. "
"Can be lowered to turn elements that appear light gray white. "
"1.0 deactivates whitening.",
)
args = parser.parse_args()
validate_config(args)
main(config=args)