-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlenstag.py
More file actions
executable file
·150 lines (120 loc) · 4.5 KB
/
lenstag.py
File metadata and controls
executable file
·150 lines (120 loc) · 4.5 KB
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
#!/usr/bin/python
# lenstag.py
# Copyright 2010 Aleksandr Milewski <zandr@milewski.org>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
from collections import defaultdict
import subprocess
import optparse
import ConfigParser
usage = "usage: %prog [options] lens.ini imagefile1 [imagefile2...]"
parser=optparse.OptionParser(usage=usage)
parser.add_option("-g", "--geotag", type="string", dest="tracklog",
help="GeoTag using a track log file (i.e. GPX)",
)
parser.add_option("-w", "--wide-open", action="store_true", default=False,
dest="wide_open",
help="Set Fnumber tag assuming the lens was wide open",
)
parser.add_option("-e", "--exif-tool", type="string", dest="exiftool",
default="exiftool",
help="Path to exiftool binary"
)
parser.add_option("-v", "--verbose", action="store_true", default=False,
help="Be verbose, passes -v to exiftool"
)
(options, args) = parser.parse_args()
print options.exiftool
def usage():
parser.print_help()
exit(2)
if len(args) < 2:
print "You must specify a lens file and at least one image file."
usage()
if "ini" not in args[0]:
print args[0]
print "The first argument must be a lens .ini file."
usage()
cp = ConfigParser.SafeConfigParser()
cp.read(args.pop(0))
lensdata = dict(cp.items("Lens"))
# Try to guess lens type if it's missing
if 't' not in lensdata:
if 'fl' in lensdata:
lensdata['t'] = "Fixed"
elif (lensdata['fls'] == lensdata['fll']):
lensdata['t'] = "Fixed"
lensdata['fl'] = lensdata['fls']
else:
lensdata['t'] = "Zoom"
# Populate min/max fields based on type, abort if type is unknown
if lensdata['t'] == "Fixed":
lensdata['fls'] = lensdata['fl']
lensdata['fll'] = lensdata['fl']
lensdata['amxs'] = lensdata['amx']
lensdata['amxl'] = lensdata['amx']
elif lensdata['t'] == "Zoom":
lensdata['amx'] = lensdata['amxs']
else:
print 'Lens type must be "Fixed" or "Zoom"'
usage()
# Set no_iris flag if we should populate FNumber
if 'ni' not in lensdata:
if (lensdata['amn'] == lensdata['amx']):
lensdata['ni'] = True
else:
lensdata['ni'] = False
# Conversely, fill in missing amn if no_iris
if 'amn' not in lensdata and lensdata['ni']:
lensdata['amn'] = lensdata['amx']
def set_tags(lens, tags, extension):
tags['LensModel']=lens['d']
if (extension == ".ORF"):
tags['MaxApertureValue'] = lens['amxs']
tags['MaxApertureAtMinFocal'] = lens['amxs']
tags['MaxApertureAtMaxFocal'] = lens['amxl']
tags['MinFocalLength'] = lens['fls']
tags['MaxFocalLength'] = lens['fll']
else:
tags['MaxAperture'] = lens['amxs']
tags['MinAperture'] = lens['amn']
tags['LongFocal'] = lens['fll']
tags['ShortFocal'] = lens['fls']
if (extension == ".CR2"):
tags['FocalType'] = lens['t']
if lens['ni'] or options.wide_open:
tags['FNumber'] = lens['amx']
if (extension == ".CR2"):
tags['ApertureValue'] = lens['amx']
if lens['t'] == "Fixed":
tags['FocalLength'] = lens['fl']
filetypes = defaultdict(list)
for file in args:
basename, ext = os.path.splitext(file)
ext = ext.upper()
filetypes[ext].append(file)
for extension, files in filetypes.iteritems():
exiftags = dict()
# GeoTag
if options.tracklog:
exiftags['geotag'] = options.tracklog
set_tags(lensdata, exiftags, extension)
arglist = list()
for option, argument in exiftags.iteritems():
arglist.append("-" + option + "=" + argument)
if options.verbose: arglist.insert(0, "-v")
arglist.insert(0, options.exiftool)
arglist.extend(files)
print "Calling exiftool on " + str(len(files)) + " " + extension + " file(s)..."
result = subprocess.call(arglist)