-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphotostats.py
100 lines (79 loc) · 2.28 KB
/
photostats.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Released under the MIT license
Copyright (c) 2014, Jason Millward
@category misc
@author Jason Millward <jason@jcode.me>
@license http://opensource.org/licenses/MIT
Usage:
stats.py <EXIF Attr>... [--dir=<dir>]
"""
import exifread
import docopt
import unicodedata
from os import listdir
from os.path import isfile, join
__version__ = "0.1"
def findimages(directory):
try:
result = [ f for f in listdir(directory) if isfile(join(directory,f)) ]
except OSError:
print "Directory not found"
result = []
finally:
return result
def returnattribute(image, attribute):
try:
f = open(image, 'rb')
tags = exifread.process_file(f)
result = str(tags[attribute])
except KeyError:
#print "Attribute: " + attribute
result = None
finally:
return result
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
def parseresults(imageresults):
for r in imageresults:
print r
b = imageresults[r].keys()
b.sort()
for v in b:
count = "+" * imageresults[r][v]
print "\t{0}\t {1}".format(v, count)
print ""
def statistics(arguments):
imageresults = {}
directory = arguments["--dir"]
if directory is None:
directory = "."
for f in findimages(directory):
if f.endswith(".jpg"):
for e in arguments["<EXIF Attr>"]:
attribute = "EXIF %s" % e
val = returnattribute( join(directory, f), attribute)
if val is not None:
if is_number(val):
val = int(val)
if e in imageresults:
if val in imageresults[e]:
imageresults[e][val] += 1
else:
imageresults[e][val] = 1
else:
imageresults[e] = {}
imageresults[e][val] = 1
parseresults(imageresults)
if __name__ == '__main__':
arguments = docopt.docopt(__doc__, version=__version__)
statistics(arguments)