Added normalized flag to ExifTool.asdict()

This commit is contained in:
Rhet Turnbull
2021-04-25 08:31:08 -07:00
parent 92d9dfaef2
commit 3d26206d91
3 changed files with 20 additions and 3 deletions

View File

@@ -308,12 +308,13 @@ class ExifTool:
ver, _, _ = self.run_commands("-ver", no_file=True) ver, _, _ = self.run_commands("-ver", no_file=True)
return ver.decode("utf-8") return ver.decode("utf-8")
def asdict(self, tag_groups=True): def asdict(self, tag_groups=True, normalized=False):
"""return dictionary of all EXIF tags and values from exiftool """return dictionary of all EXIF tags and values from exiftool
returns empty dict if no tags returns empty dict if no tags
Args: Args:
tag_groups: if True (default), dict keys have tag groups, e.g. "IPTC:Keywords"; if False, drops groups from keys, e.g. "Keywords" tag_groups: if True (default), dict keys have tag groups, e.g. "IPTC:Keywords"; if False, drops groups from keys, e.g. "Keywords"
normalized: if True, dict keys are all normalized to lower case (default is False)
""" """
json_str, _, _ = self.run_commands("-json") json_str, _, _ = self.run_commands("-json")
if not json_str: if not json_str:
@@ -334,6 +335,10 @@ class ExifTool:
k = re.sub(r".*:", "", k) k = re.sub(r".*:", "", k)
exif_new[k] = v exif_new[k] = v
exifdict = exif_new exifdict = exif_new
if normalized:
exifdict = {k.lower(): v for (k, v) in exifdict.items()}
return exifdict return exifdict
def json(self): def json(self):
@@ -360,3 +365,7 @@ class ExifTool:
elif self._commands: elif self._commands:
# run_commands sets self.warning and self.error as needed # run_commands sets self.warning and self.error as needed
self.run_commands(*self._commands) self.run_commands(*self._commands)

View File

@@ -1082,8 +1082,7 @@ class PhotoTemplate:
return [] return []
exif = ExifTool(self.photo.path, exiftool=self.exiftool_path) exif = ExifTool(self.photo.path, exiftool=self.exiftool_path)
exifdict = exif.asdict() exifdict = exif.asdict(normalized=True)
exifdict = {k.lower(): v for (k, v) in exifdict.items()}
subfield = subfield.lower() subfield = subfield.lower()
if subfield in exifdict: if subfield in exifdict:
values = exifdict[subfield] values = exifdict[subfield]

View File

@@ -349,6 +349,15 @@ def test_as_dict():
assert exifdata["XMP:TagsList"] == "wedding" assert exifdata["XMP:TagsList"] == "wedding"
def test_as_dict_normalized():
import osxphotos.exiftool
exif1 = osxphotos.exiftool.ExifTool(TEST_FILE_ONE_KEYWORD)
exifdata = exif1.asdict(normalized=True)
assert exifdata["xmp:tagslist"] == "wedding"
assert "XMP:TagsList" not in exifdata
def test_as_dict_no_tag_groups(): def test_as_dict_no_tag_groups():
import osxphotos.exiftool import osxphotos.exiftool