Skip to content

Commit 46b2913

Browse files
Thomas DietrichThomas Dietrich
authored andcommitted
Check for correct filename, Rename only if incorrect
1 parent 47679b8 commit 46b2913

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

EXIFDateTimeAdjust.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ def setDatetimeFileCMA(filepath, timestamp):
9898
handle.close()
9999

100100

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+
101110

102111
def getDatetimeFilename(filepath):
103112
filename = os.path.splitext(os.path.basename(filepath))[0].lower()
@@ -132,27 +141,22 @@ def renameFileDatetime(filepath, timestamp):
132141
filename = os.path.splitext(os.path.basename(filepath))[0]
133142
ext = os.path.splitext(os.path.basename(filepath))[1][1:].strip().lower()
134143
destination = path + os.sep + timestampString + '.' + ext
135-
if filepath == destination:
136-
print Fore.YELLOW + 'no rename needed.' + Style.RESET_ALL
137-
return 0
138144
if not os.path.exists(destination):
139145
shutil.move(filepath, destination)
140-
return destination
141146
else:
142147
for i in range(1, 100):
143148
destination = path + os.sep + timestampString + "_" + str(i).zfill(2) + '.' + ext
144149
if not os.path.exists(destination):
145150
shutil.move(filepath, destination)
146-
print Fore.YELLOW + 'renamed to "' + destination + '"' + Style.RESET_ALL
147-
return destination
148151
break
149152
else:
150153
print Fore.RED + 'file "' + filepath + '" could not be renamed!' + Style.RESET_ALL
151154
return 0
152-
155+
print Fore.YELLOW + 'renamed to "' + destination + '"' + Style.RESET_ALL
156+
return destination
153157

154158
for imageFile in reversed(getJpgFiles(path)):
155-
print Style.BRIGHT + "\n" + "-"*50 + Style.RESET_ALL
159+
print Style.DIM + "\n" + "-"*50 + Style.RESET_ALL
156160
print imageFile
157161

158162
datetimeExif = getDatetimeExif(imageFile)
@@ -165,25 +169,28 @@ def renameFileDatetime(filepath, timestamp):
165169
print "Filename1970:\t" + str(datetimeFilename1970)
166170

167171
if datetimeExif and datetimeFilename:
168-
print "--> elected:\t" + str(datetimeExif)
172+
print Style.BRIGHT + "--> elected:\t" + str(datetimeExif) + Style.RESET_ALL
169173
#if datetimeExif == datetimeFilename: #...because we are not in a perfect world
170174
if (datetimeFilename - timedelta(seconds=300)) < datetimeExif < (datetimeFilename + timedelta(seconds=300)):
171175
if datetimeExif == datetimeFileCreated:
172176
#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)
175181
#TODO differentiate between detected and correct timestamp
176182
else:
177183
print "case 1: EXIF Photo.DateTimeOriginal and filename timestamp match"
178184
print Fore.CYAN + "correcting file creation date and filename..." + Style.RESET_ALL
179185
setDatetimeFileCMA(imageFile, datetimeExif)
180-
renameFileDatetime(imageFile, datetimeExif)
186+
if not checkCorrectFilename(imageFile):
187+
renameFileDatetime(imageFile, datetimeExif)
181188
else:
182189
print "case 2: EXIF Photo.DateTimeOriginal and filename timestamp are different"
183190
print Fore.RED + "manual correction needed!" + Style.RESET_ALL
184191
print "continue?", raw_input()
185192
elif datetimeExif and not datetimeFilename:
186-
print "--> elected:\t" + str(datetimeExif)
193+
print Style.BRIGHT + "--> elected:\t" + str(datetimeExif) + Style.RESET_ALL
187194
if datetimeExif == datetimeFileCreated:
188195
print "case 3: EXIF Photo.DateTimeOriginal and file creation date match"
189196
print Fore.CYAN + "correcting filename timestamp..." + Style.RESET_ALL
@@ -197,7 +204,7 @@ def renameFileDatetime(filepath, timestamp):
197204
setDatetimeFileCMA(imageFile, datetimeExif)
198205
renameFileDatetime(imageFile, datetimeExif)
199206
elif not datetimeExif and datetimeFilename:
200-
print "--> elected:\t" + str(datetimeFilename)
207+
print Style.BRIGHT + "--> elected:\t" + str(datetimeFilename) + Style.RESET_ALL
201208
if datetimeFilename == datetimeFileCreated:
202209
print "case 5: filename timestamp and file creation date match"
203210
print Fore.CYAN + "correcting EXIF Photo.DateTimeOriginal..." + Style.RESET_ALL
@@ -211,7 +218,7 @@ def renameFileDatetime(filepath, timestamp):
211218
setDatetimeExif(imageFile, datetimeFilename)
212219
setDatetimeFileCMA(imageFile, datetimeFilename)
213220
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
215222
print "case 7"
216223
print Fore.RED + "only..." + Style.RESET_ALL
217224
print "continue?", raw_input()
@@ -221,10 +228,8 @@ def renameFileDatetime(filepath, timestamp):
221228
elif not datetimeExif and not datetimeFilename and datetimeFileCreated:
222229
print "--> elected:\t" + str(datetimeFileCreated)
223230
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
225232
print "continue?", raw_input()
226-
#setDatetimeExif(imageFile, datetimeFileCreated)
227-
#renameFileDatetime(imageFile, datetimeFileCreated)
228233
else:
229234
print Fore.RED + "no clue... try yourself" + Style.RESET_ALL
230235
print "(go set the filename, than tickle me again)"

0 commit comments

Comments
 (0)