-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathextract_metadata.py
More file actions
69 lines (61 loc) · 2.12 KB
/
extract_metadata.py
File metadata and controls
69 lines (61 loc) · 2.12 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
# coding=utf-8
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pathlib import Path
from argparse import ArgumentParser, Namespace
import pickle
from PIL import Image, ExifTags, TiffImagePlugin
import subprocess
import json
from tqdm import tqdm
parser = ArgumentParser(description="Extract metadata parameters")
parser.add_argument('images', type=Path)
parser.add_argument('output', type=Path)
parser.add_argument('--exif_tool_path', type=str, default="exiftool")
args = parser.parse_args()
metadatas = {
}
iso_tags = ["Sony ISO", "ISO"]
exposure_tags = ["Sony Exposure Time 2", "Exposure Time"]
aperature_tags = ["FNumber", "Sony F Number 2"]
def get_value(data, tags):
vs = [data[t] for t in tags if t in data]
return vs[0] if len(vs) > 0 else -1
for path in tqdm(args.images.iterdir()):
try:
img = Image.open(path)
except:
print(path, " is not an image")
process = subprocess.Popen(
[args.exif_tool_path,str(path)],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
exif = {
ExifTags.TAGS[k]: float(v) if isinstance(v, TiffImagePlugin.IFDRational) else v
for k, v in img._getexif().items()
if k in ExifTags.TAGS
}
for tag in process.stdout:
line = tag.strip().split(':')
exif[line[0].strip()] = line[-1].strip()
data = dict(
iso=get_value(exif, iso_tags),
exposure=get_value(exif, exposure_tags),
aperature=get_value(exif, aperature_tags),
)
# print(exif)
metadatas[path.name] = data
with args.output.open("w") as f:
json.dump(metadatas, f)