@@ -98,6 +98,15 @@ def setDatetimeFileCMA(filepath, timestamp):
98
98
handle .close ()
99
99
100
100
101
+ def checkCorrectFilename (filepath ):
102
+ filename = os .path .splitext (os .path .basename (filepath ))[0 ].lower ()
103
+ try :
104
+ # 2014-01-01_00.33.46[_01](.jpg)
105
+ t = datetime .strptime (filename [0 :19 ], "%Y-%m-%d_%H.%M.%S" )
106
+ except ValueError :
107
+ return False
108
+ return True
109
+
101
110
102
111
def getDatetimeFilename (filepath ):
103
112
filename = os .path .splitext (os .path .basename (filepath ))[0 ].lower ()
@@ -132,27 +141,22 @@ def renameFileDatetime(filepath, timestamp):
132
141
filename = os .path .splitext (os .path .basename (filepath ))[0 ]
133
142
ext = os .path .splitext (os .path .basename (filepath ))[1 ][1 :].strip ().lower ()
134
143
destination = path + os .sep + timestampString + '.' + ext
135
- if filepath == destination :
136
- print Fore .YELLOW + 'no rename needed.' + Style .RESET_ALL
137
- return 0
138
144
if not os .path .exists (destination ):
139
145
shutil .move (filepath , destination )
140
- return destination
141
146
else :
142
147
for i in range (1 , 100 ):
143
148
destination = path + os .sep + timestampString + "_" + str (i ).zfill (2 ) + '.' + ext
144
149
if not os .path .exists (destination ):
145
150
shutil .move (filepath , destination )
146
- print Fore .YELLOW + 'renamed to "' + destination + '"' + Style .RESET_ALL
147
- return destination
148
151
break
149
152
else :
150
153
print Fore .RED + 'file "' + filepath + '" could not be renamed!' + Style .RESET_ALL
151
154
return 0
152
-
155
+ print Fore .YELLOW + 'renamed to "' + destination + '"' + Style .RESET_ALL
156
+ return destination
153
157
154
158
for imageFile in reversed (getJpgFiles (path )):
155
- print Style .BRIGHT + "\n " + "-" * 50 + Style .RESET_ALL
159
+ print Style .DIM + "\n " + "-" * 50 + Style .RESET_ALL
156
160
print imageFile
157
161
158
162
datetimeExif = getDatetimeExif (imageFile )
@@ -165,25 +169,28 @@ def renameFileDatetime(filepath, timestamp):
165
169
print "Filename1970:\t " + str (datetimeFilename1970 )
166
170
167
171
if datetimeExif and datetimeFilename :
168
- print "--> elected:\t " + str (datetimeExif )
172
+ print Style . BRIGHT + "--> elected:\t " + str (datetimeExif ) + Style . RESET_ALL
169
173
#if datetimeExif == datetimeFilename: #...because we are not in a perfect world
170
174
if (datetimeFilename - timedelta (seconds = 300 )) < datetimeExif < (datetimeFilename + timedelta (seconds = 300 )):
171
175
if datetimeExif == datetimeFileCreated :
172
176
#print "case 0: EXIF Photo.DateTimeOriginal, filename timestamp and file creation date match"
173
- print Fore .GREEN + "nothing to do here." + Style .RESET_ALL
174
- renameFileDatetime (imageFile , datetimeExif )
177
+ if checkCorrectFilename (imageFile ):
178
+ print Fore .GREEN + "nothing to do here." + Style .RESET_ALL
179
+ else :
180
+ renameFileDatetime (imageFile , datetimeExif )
175
181
#TODO differentiate between detected and correct timestamp
176
182
else :
177
183
print "case 1: EXIF Photo.DateTimeOriginal and filename timestamp match"
178
184
print Fore .CYAN + "correcting file creation date and filename..." + Style .RESET_ALL
179
185
setDatetimeFileCMA (imageFile , datetimeExif )
180
- renameFileDatetime (imageFile , datetimeExif )
186
+ if not checkCorrectFilename (imageFile ):
187
+ renameFileDatetime (imageFile , datetimeExif )
181
188
else :
182
189
print "case 2: EXIF Photo.DateTimeOriginal and filename timestamp are different"
183
190
print Fore .RED + "manual correction needed!" + Style .RESET_ALL
184
191
print "continue?" , raw_input ()
185
192
elif datetimeExif and not datetimeFilename :
186
- print "--> elected:\t " + str (datetimeExif )
193
+ print Style . BRIGHT + "--> elected:\t " + str (datetimeExif ) + Style . RESET_ALL
187
194
if datetimeExif == datetimeFileCreated :
188
195
print "case 3: EXIF Photo.DateTimeOriginal and file creation date match"
189
196
print Fore .CYAN + "correcting filename timestamp..." + Style .RESET_ALL
@@ -197,7 +204,7 @@ def renameFileDatetime(filepath, timestamp):
197
204
setDatetimeFileCMA (imageFile , datetimeExif )
198
205
renameFileDatetime (imageFile , datetimeExif )
199
206
elif not datetimeExif and datetimeFilename :
200
- print "--> elected:\t " + str (datetimeFilename )
207
+ print Style . BRIGHT + "--> elected:\t " + str (datetimeFilename ) + Style . RESET_ALL
201
208
if datetimeFilename == datetimeFileCreated :
202
209
print "case 5: filename timestamp and file creation date match"
203
210
print Fore .CYAN + "correcting EXIF Photo.DateTimeOriginal..." + Style .RESET_ALL
@@ -211,7 +218,7 @@ def renameFileDatetime(filepath, timestamp):
211
218
setDatetimeExif (imageFile , datetimeFilename )
212
219
setDatetimeFileCMA (imageFile , datetimeFilename )
213
220
elif not datetimeExif and not datetimeFilename and datetimeFilename1970 :
214
- print "--> elected:\t " + str (datetimeFilename1970 )
221
+ print Style . BRIGHT + "--> elected:\t " + str (datetimeFilename1970 ) + Style . RESET_ALL
215
222
print "case 7"
216
223
print Fore .RED + "only..." + Style .RESET_ALL
217
224
print "continue?" , raw_input ()
@@ -221,10 +228,8 @@ def renameFileDatetime(filepath, timestamp):
221
228
elif not datetimeExif and not datetimeFilename and datetimeFileCreated :
222
229
print "--> elected:\t " + str (datetimeFileCreated )
223
230
print "case 8: file created timestamp ONLY"
224
- print Fore .RED + "only... " + Style .RESET_ALL
231
+ print Fore .RED + "manual correction needed! " + Style .RESET_ALL
225
232
print "continue?" , raw_input ()
226
- #setDatetimeExif(imageFile, datetimeFileCreated)
227
- #renameFileDatetime(imageFile, datetimeFileCreated)
228
233
else :
229
234
print Fore .RED + "no clue... try yourself" + Style .RESET_ALL
230
235
print "(go set the filename, than tickle me again)"
0 commit comments