-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathexif_extractor.py
72 lines (61 loc) · 2.76 KB
/
exif_extractor.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
#!/usr/bin/env python
import sys, os, optparse
from PIL import Image
from PIL.ExifTags import TAGS
from common_tools import *
def getExif(image_file, save=True, verbose=True):
'''Get image file EXIF metadata'''
if not os.path.isfile(image_file):
sys.exit("%s is not a valid image file!")
data = "Time: %d/%d/%d %d : %d : %d. Found the following Exif data for the image %s:\n\n" % (now.year, now.month,
now.day, now.hour, now.minute,
now.second, name)
img = Image.open(image_file)
info = img._getexif()
exif_data = {}
if info:
for (tag, value) in info.items():
decoded = TAGS.get(tag, tag)
if type(value) is bytes:
try:
exif_data[decoded] = value.decode("utf-8")
except:
pass
else:
exif_data[decoded] = value
else:
sys.exit("No EXIF data found!")
for key in exif_data:
data += "{} : {}\n".format(key, exif_data[key])
if save:
name = getFileName(image_file)
tgt = name + ".txt"
saveResult(tgt, data)
if verbose:
print("Found the following exif data:\n", data)
if __name__ == "__main__":
print('\n\n ##########A simple Python script to extract exif metadata #########')
print(' # Coded by monrocoury #')
print(' # most images contain Exif Metadata #')
print(' # for more info refer to Google/wiki #')
print(' ###################################################################\n\n')
parser = optparse.OptionParser("Usage: python exif_extractor.py -i <image path> -s <True or False> -v <True or False>")
parser.add_option("-i", dest="image_path", type="string", help="provide the full path to the image file. eg: E:\images\img_1.jpg")
parser.add_option("-s", dest="save", type="string", help="(optional) save the exif data as a text file? default True")
parser.add_option("-v", dest="verbose", type="string", help="(optional) if False results won't be displayed in the console. default True")
(options, args) = parser.parse_args()
path = options.image_path
if not path:
print("please provide the path to the image file!")
sys.exit(parser.usage)
save = options.save
if not save:
save = True
else:
save = eval(save.title())
verbose = options.verbose
if not verbose:
verbose = False
else:
verbose = eval(verbose.title())
getExif(path, save, verbose)