2015-11-29 14:34:20 +01:00
|
|
|
import os.path
|
2015-12-06 03:17:15 +01:00
|
|
|
import ntpath
|
2015-11-29 14:34:20 +01:00
|
|
|
import exifread
|
2015-12-06 01:52:21 +01:00
|
|
|
from time import localtime, strftime, strptime, mktime
|
2015-11-29 14:34:20 +01:00
|
|
|
import shutil
|
|
|
|
|
|
|
|
minEventDelta = 60 * 60 * 24 * 4 # 4 days in seconds
|
2015-12-06 01:52:21 +01:00
|
|
|
unknownDateFolderName = "Datum unbekannt"
|
2015-11-29 14:34:20 +01:00
|
|
|
|
|
|
|
def getMinimumCreationTime(exif_data):
|
|
|
|
creationTime = None
|
|
|
|
dateTime = exif_data.get('DateTime')
|
|
|
|
dateTimeOriginal = exif_data.get('EXIF DateTimeOriginal')
|
|
|
|
dateTimeDigitized = exif_data.get('EXIF DateTimeDigitized')
|
|
|
|
|
|
|
|
# 3 differnt time fields that can be set independently result in 9 if-cases
|
|
|
|
if (dateTime is None):
|
|
|
|
if (dateTimeOriginal is None):
|
|
|
|
# case 1/9: dateTime, dateTimeOriginal, and dateTimeDigitized = None
|
|
|
|
# case 2/9: dateTime and dateTimeOriginal = None, then use dateTimeDigitized
|
|
|
|
creationTime = dateTimeDigitized
|
|
|
|
else:
|
|
|
|
# case 3/9: dateTime and dateTimeDigitized = None, then use dateTimeOriginal
|
|
|
|
# case 4/9: dateTime = None, prefere dateTimeOriginal over dateTimeDigitized
|
|
|
|
creationTime = dateTimeOriginal
|
|
|
|
else:
|
|
|
|
# case 5-9: when creationTime is set, prefere it over the others
|
|
|
|
creationTime = dateTime
|
|
|
|
|
|
|
|
return creationTime
|
|
|
|
|
2015-12-06 01:52:21 +01:00
|
|
|
def postprocessImage(images, imageDirectory, fileName):
|
|
|
|
imagePath = os.path.join(imageDirectory, fileName)
|
2015-11-29 14:34:20 +01:00
|
|
|
image = open(imagePath, 'rb')
|
2015-12-06 01:52:21 +01:00
|
|
|
creationTime = None
|
2015-11-29 14:34:20 +01:00
|
|
|
try:
|
|
|
|
exifTags = exifread.process_file(image, details=False)
|
2015-12-06 01:52:21 +01:00
|
|
|
creationTime = getMinimumCreationTime(exifTags)
|
2015-11-29 14:34:20 +01:00
|
|
|
except:
|
|
|
|
print("invalid exif tags for " + fileName)
|
|
|
|
|
|
|
|
# distinct different time types
|
|
|
|
if creationTime is None:
|
2015-12-06 01:52:21 +01:00
|
|
|
creationTime = localtime(os.path.getctime(imagePath))
|
2015-11-29 14:34:20 +01:00
|
|
|
else:
|
2015-12-06 01:52:21 +01:00
|
|
|
try:
|
|
|
|
creationTime = strptime(str(creationTime), "%Y:%m:%d %H:%M:%S")
|
|
|
|
except:
|
|
|
|
creationTime = localtime(os.path.getctime(imagePath))
|
2015-11-29 14:34:20 +01:00
|
|
|
|
2015-12-06 01:52:21 +01:00
|
|
|
images.append((mktime(creationTime), imagePath))
|
|
|
|
image.close()
|
2015-11-29 14:34:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
def createNewFolder(destinationRoot, year, eventNumber):
|
|
|
|
yearPath = os.path.join(destinationRoot, year)
|
|
|
|
if not os.path.exists(yearPath):
|
|
|
|
os.mkdir(yearPath)
|
|
|
|
eventPath = os.path.join(yearPath, str(eventNumber))
|
|
|
|
if not os.path.exists(eventPath):
|
|
|
|
os.mkdir(eventPath)
|
|
|
|
|
2015-12-06 01:52:21 +01:00
|
|
|
def createUnknownDateFolder(destinationRoot):
|
|
|
|
path = os.path.join(destinationRoot, unknownDateFolderName)
|
|
|
|
if not os.path.exists(path):
|
|
|
|
os.mkdir(path)
|
|
|
|
|
2015-11-29 14:34:20 +01:00
|
|
|
|
|
|
|
def writeImages(images, destinationRoot):
|
|
|
|
sortedImages = sorted(images)
|
|
|
|
previousTime = None
|
|
|
|
eventNumber = 0
|
2015-12-06 01:52:21 +01:00
|
|
|
today = strftime("%d/%m/%Y")
|
2015-11-29 14:34:20 +01:00
|
|
|
|
|
|
|
for imageTuple in sortedImages:
|
2015-12-06 03:17:15 +01:00
|
|
|
destination = ""
|
|
|
|
destinationFilePath = ""
|
2015-12-06 01:52:21 +01:00
|
|
|
t = localtime(imageTuple[0])
|
|
|
|
year = strftime("%Y", t)
|
|
|
|
creationDate = strftime("%d/%m/%Y", t)
|
2015-12-06 03:17:15 +01:00
|
|
|
fileName = ntpath.basename(imageTuple[1])
|
|
|
|
|
2015-12-06 01:52:21 +01:00
|
|
|
if(creationDate == today):
|
|
|
|
createUnknownDateFolder(destinationRoot)
|
|
|
|
destination = os.path.join(destinationRoot, unknownDateFolderName)
|
2015-12-06 03:17:15 +01:00
|
|
|
destinationFilePath = os.path.join(destination, fileName)
|
|
|
|
|
2015-12-06 01:52:21 +01:00
|
|
|
else:
|
|
|
|
if (previousTime == None) or ((previousTime + minEventDelta) < imageTuple[0]):
|
|
|
|
previousTime = imageTuple[0]
|
|
|
|
eventNumber = eventNumber + 1
|
|
|
|
createNewFolder(destinationRoot, year, eventNumber)
|
|
|
|
|
2015-11-29 14:34:20 +01:00
|
|
|
previousTime = imageTuple[0]
|
|
|
|
|
2015-12-06 01:52:21 +01:00
|
|
|
destination = os.path.join(destinationRoot, year, str(eventNumber))
|
|
|
|
# it may be possible that an event covers 2 years.
|
|
|
|
# in such a case put all the images to the even in the old year
|
|
|
|
if not (os.path.exists(destination)):
|
2015-12-06 03:17:15 +01:00
|
|
|
destination = os.path.join(destinationRoot, str(int(year) - 1), str(eventNumber))
|
2015-12-06 01:52:21 +01:00
|
|
|
|
2015-12-06 03:17:15 +01:00
|
|
|
destinationFilePath = os.path.join(destination, fileName)
|
|
|
|
|
|
|
|
if not (os.path.exists(destinationFilePath)):
|
2015-12-06 01:52:21 +01:00
|
|
|
shutil.move(imageTuple[1], destination)
|
2015-12-06 03:17:15 +01:00
|
|
|
else:
|
|
|
|
if (os.path.exists(imageTuple[1])):
|
|
|
|
os.remove(imageTuple[1])
|
2015-12-06 01:52:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def postprocessImages(imageDirectory):
|
|
|
|
images = []
|
|
|
|
for root, dirs, files in os.walk(imageDirectory):
|
|
|
|
for file in files:
|
|
|
|
postprocessImage(images, imageDirectory, file)
|
|
|
|
|
|
|
|
writeImages(images, imageDirectory)
|