-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimageInfo.py
executable file
·113 lines (96 loc) · 3.67 KB
/
imageInfo.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#
# Copyright (c) 2013 Bhautik J Joshi ([email protected])
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# just a simplified wrapper around IPTCInfo; effectively just wrapping
# semantics for ease-of-use
import findLibs
from iptcinfo import IPTCInfo
from minimal_exif_reader import MinimalExifReader
import datetime
iptcMap = {
"copyrightNotice" : "copyright notice",
"author" : "by-line",
"title" : "object name",
"tags" : "keywords", #[key1, key2, ...]
"description" : "caption/abstract",
"dateCreated" : "date created", #20130310
"timeCreated" : "time created" #140658
}
def nowStamp():
now = datetime.datetime.today()
date = '%04d%02d%02d' % (now.year, now.month, now.day)
time = '%02d%02d%02d' % (now.hour, now.minute, now.second)
return date, time
class Image:
def __init__(self, filename):
self.filename = filename
self.info = None
self.exif = None
try:
self.info = IPTCInfo(self.filename, force=True)
except:
print "cannot initialize IPTCInfo!"
pass
try:
self.exif = MinimalExifReader(self.filename)
except:
print "cannot initialize exif reader"
pass
# if we can't create either reader, give up
if self.exif == None and self.info == None:
print "cannot read file metadata"
raise
# at a minimum, we set a datestamp if one doesn't exist
# set to current date & time
exifDateStamp = None
if self.exif != None:
exifDateStamp = self.exif.dateTimeOriginal("%Y%m%d-%H%M%S")
if exifDateStamp == "":
exifDateStamp = None
iptcDate = self.getData("dateCreated")
iptcTime = self.getData("timeCreated")
if exifDateStamp != None and \
iptcDate == None and \
iptcTime == None:
datestamp = exifDateStamp.split('-')
self.info.data[iptcMap['dateCreated']] = datestamp[0]
self.info.data[iptcMap['timeCreated']] = datestamp[1]
elif exifDateStamp == None and \
iptcDate == None and \
iptcTime == None:
date, time = nowStamp()
self.info.data[iptcMap['dateCreated']] = date
self.info.data[iptcMap['timeCreated']] = time
def availableFields(self):
return iptcMap.keys()
def setData(self, field, data):
if field in iptcMap:
self.info.data[iptcMap[field]] = data
def getData(self, field):
if self.info != None:
if field in iptcMap:
return self.info.data[iptcMap[field]]
return None
def writeFile(self):
self.info.save()
def dump(self):
for f in self.availableFields():
print f + " : " + str(self.getData(f))