-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmrcal-to-cahvor
executable file
·99 lines (79 loc) · 3.51 KB
/
mrcal-to-cahvor
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
#!/usr/bin/env python3
# Copyright (c) 2017-2023 California Institute of Technology ("Caltech"). U.S.
# Government sponsorship acknowledged. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
r'''Converts a cameramodel to the cahvor file format
SYNOPSIS
$ mrcal-to-cahvor model1.cameramodel model2.cameramodel
Wrote model1.cahvor
Wrote model2.cahvor
File formats supported by mrcal are described at
https://mrcal.secretsauce.net/cameramodels.html#cameramodel-file-formats
This tool converts the given model(s) to the cahvor file format. No changes to
the content are made; this is purely a format converter (the
mrcal-convert-lensmodel tool fits different lens models instead). Model
filenames are given on the commandline. Output is written to the same directory,
with the same filename, but with a .cahvor extension.
If the model is omitted or given as "-", the input is read from standard input,
and the output is written to standard output
'''
import sys
import argparse
import re
import os
def parse_args():
parser = \
argparse.ArgumentParser(description = __doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--force', '-f',
action='store_true',
default=False,
help='''By default existing files are not overwritten. Pass --force to overwrite them
without complaint''')
parser.add_argument('--outdir',
required=False,
type=lambda d: d if os.path.isdir(d) else \
parser.error("--outdir requires an existing directory as the arg, but got '{}'".format(d)),
help='''Directory to write the output models into. If omitted, we write the output
models to the same directory as the input models''')
parser.add_argument('model',
default=['-'],
nargs='*',
type=str,
help='''Input camera model''')
return parser.parse_args()
args = parse_args()
# arg-parsing is done before the imports so that --help works without building
# stuff, so that I can generate the manpages and README
Nstdin = sum(1 for m in args.model if m=='-')
if Nstdin > 1:
print(f"At most one model can be read from standard input ('-'), but I got {Nstdin}", file=sys.stderr)
sys.exit(1)
import mrcal
for model in args.model:
if model == '-':
try:
m = mrcal.cameramodel(model)
except KeyboardInterrupt:
sys.exit(1)
m.write(sys.stdout, cahvor = True)
else:
base,extension = os.path.splitext(model)
if extension.lower() == '.cahvor':
print("Input file is already in the cahvor format (judging from the filename). Doing nothing",
file=sys.stderr)
sys.exit(0)
if args.outdir is not None:
base = args.outdir + '/' + os.path.split(base)[1]
filename_out = base + '.cahvor'
if not args.force and os.path.exists(filename_out):
print(f"Target model '{filename_out}' already exists. Doing nothing with this model. Pass -f to overwrite",
file=sys.stderr)
else:
m = mrcal.cameramodel(model)
m.write(filename_out)
print("Wrote " + filename_out)