-
-
Save feliperyan/72faf1d34a2a9d1a72a001f717c08e4d to your computer and use it in GitHub Desktop.
Read EXIF data from an image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
"""Read EXIF data from an image | |
https://gist.github.com/ishahid/137f8724d8cff2304dffd69f3ade26d7 | |
""" | |
try: | |
import os, sys | |
# import json | |
from PIL import Image | |
from PIL.ExifTags import TAGS | |
except ImportError as exc: | |
print 'Required dependencies not installed.' | |
print 'Run the following commands...' | |
print '$ pip install pillow' | |
print exc | |
exit() | |
def get_exif(fn): | |
ret = {} | |
try: | |
i = Image.open(fn) | |
info = i._getexif() | |
except: | |
return ret | |
if info: | |
for tag, value in info.items(): | |
decoded = TAGS.get(tag, tag) | |
ret[decoded] = value | |
return ret | |
else: | |
return ret | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
print 'Usage: exif.py <file>' + os.linesep | |
else: | |
file_name = sys.argv[1] | |
data = get_exif(file_name) | |
if data: | |
for key in sorted(data.keys()): | |
print '%s: %s' % (key, data[key]) | |
else: | |
print 'No EXIF data found.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment