-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexif.py
76 lines (54 loc) · 2.11 KB
/
exif.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
"""Work with file metadata"""
# Extent to include windows tags at some point - candidate library https://github.com/james-see/iptcinfo3
import os.path as _path
from dateutil.parser import parse as _parse
from PIL import Image as _Image
from PIL import ExifTags as _ExifTags
class Image:
"""
Load exif data for an image.
Args:
fname: Image path (normpathed)
filter_keys:
A function used to filter the returned keys
For example, filter_keys=lambda f: f in ['Aperture', 'ShutterSpeed']
Methods:
date_taken:
Get the date taken as a datetime instance
year_taken:
Get the year taken
"""
def __init__(self, fname: str, filter_keys=lambda f: True):
self._fname = _path.normpath(fname)
self.tags = {}
self.gps_tags = {}
self._filter_keys = filter_keys
self._image_tags()
self._date_taken = None
def _image_tags(self):
"""
reads all exif tags into tags
"""
img = _Image.open(self._fname)
self.tags = {_ExifTags.TAGS[k]: v for k, v in img._getexif().items() if k in _ExifTags.TAGS and self._filter_keys(_ExifTags.TAGS[k])} # noqa
@property
def date_taken(self) -> "datetime.datetime": # noqa
"""Date taken as a datetime instance
Returns none if tags couldnt be read
"""
if not self.tags: return
if self._date_taken: return self._date_taken
for dt in (self.tags['DateTime'], self.tags['DateTimeOriginal'], self.tags['DateTimeDigitized']):
try:
self. _date_taken = _parse(dt.replace(':', '').replace('-', '')) # parse makes a good job of guessing, but colons across the board as a sep nakes it fail
break
except:
pass
@property
def year_taken(self) -> int:
"""Return year taken"""
return self._date_taken.year
if __name__ == '__main__':
Img = Image(r'\\nerctbctdb\shared\shared\GMEP_Restricted\WP3_Completed squares\Year4\MainSurvey\Photos\28255_VegPlots_14_D_5.jpg')
tags = Img.tags
pass