Skip to content

Instantly share code, notes, and snippets.

@bigntallmike
Created September 7, 2021 01:33
Show Gist options
  • Save bigntallmike/0cc203c6e120fbd73d8a7988aff0a14a to your computer and use it in GitHub Desktop.
Save bigntallmike/0cc203c6e120fbd73d8a7988aff0a14a to your computer and use it in GitHub Desktop.
#!/usr/bin/python3 -ttu
#
# Use exiftool to read data from image and translate to Python dictionary
#
# Copyright 2021 Michael T. Babcock <mtb-code@mikebabcock.ca>
# Released under MIT License, enjoy
import sys, os
from subprocess import run
import json
import pprint
class EXIFException(Exception):
pass
class EXIFData:
def __init__(self, filename):
if not filename == None:
self.readexifdata(filename)
def readexifdata(self, filename):
exifret = run(['exiftool', '-json', filename], capture_output=True)
if not exifret.returncode == 0:
raise EXIFException("exiftool returned {}".format(exifret.returncode))
self.exifjson = json.loads(exifret.stdout)[0]
def __getitem__(self, key):
if key in self.exifjson:
return self.exifjson[key]
return None
def test(testfile):
data = EXIFData(testfile)
pprint.pprint(data.exifjson)
print("Width: {}".format(data['ImageWidth']))
if __name__ == '__main__':
test(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment