2015-06-05 01:29:17 +02:00
|
|
|
#!/usr/bin/env python2
|
2015-06-04 04:28:47 +02:00
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import shutil
|
|
|
|
import sys
|
2015-06-05 01:29:17 +02:00
|
|
|
import time
|
2015-09-17 20:29:05 +02:00
|
|
|
import exifread
|
2015-06-04 04:28:47 +02:00
|
|
|
|
|
|
|
source = sys.argv[1]
|
|
|
|
destination = sys.argv[2]
|
2015-09-20 11:33:54 +02:00
|
|
|
|
2015-09-18 20:03:07 +02:00
|
|
|
minEventDelta = 60 * 60 * 24 * 4 # 4 days in seconds
|
2015-06-04 04:28:47 +02:00
|
|
|
|
2015-09-17 20:29:05 +02:00
|
|
|
|
2015-09-20 11:33:54 +02:00
|
|
|
def getMinimumCreationTime(exif_data):
|
2015-09-17 20:29:05 +02:00
|
|
|
creationTime = None
|
|
|
|
dateTime = exif_data.get('DateTime')
|
|
|
|
dateTimeOriginal = exif_data.get('EXIF DateTimeOriginal')
|
|
|
|
dateTimeDigitized = exif_data.get('EXIF DateTimeDigitized')
|
|
|
|
|
|
|
|
if dateTime is not None :
|
|
|
|
creationTime = dateTime
|
|
|
|
if dateTimeOriginal is not None and (creationTime == None or dateTimeOriginal < creationTime):
|
|
|
|
creationTime = dateTimeOriginal
|
|
|
|
if dateTimeDigitized is not None and (creationTime == None or dateTimeDigitized < creationTime):
|
|
|
|
creationTime = dateTimeDigitized
|
|
|
|
return creationTime
|
|
|
|
|
2015-09-20 11:33:54 +02:00
|
|
|
def postprocessImage(images, sourceDir, imageDirectory, fileName):
|
2015-09-17 20:29:05 +02:00
|
|
|
imagePath = os.path.join(sourceDir, fileName)
|
|
|
|
image = open(imagePath, 'rb')
|
|
|
|
exifTags = exifread.process_file(image)
|
2015-09-20 11:33:54 +02:00
|
|
|
creationTime = getMinimumCreationTime(exifTags)
|
2015-09-17 20:29:05 +02:00
|
|
|
|
|
|
|
# distinct different time types
|
|
|
|
if creationTime is None:
|
|
|
|
creationTime = time.gmtime(os.path.getctime(imagePath))
|
|
|
|
else:
|
|
|
|
creationTime = time.strptime(str(creationTime), "%Y:%m:%d %H:%M:%S")
|
|
|
|
|
|
|
|
print time.asctime(creationTime)
|
|
|
|
|
2015-09-18 20:03:07 +02:00
|
|
|
images.append((time.mktime(creationTime), imagePath))
|
2015-09-17 20:29:05 +02:00
|
|
|
|
|
|
|
|
2015-09-18 20:03:07 +02:00
|
|
|
def createNewFolder(destinationRoot, year, eventNumber):
|
|
|
|
yearPath = os.path.join(destinationRoot, year)
|
2015-09-17 20:29:05 +02:00
|
|
|
if not os.path.exists(yearPath):
|
|
|
|
os.mkdir(yearPath)
|
2015-09-18 20:03:07 +02:00
|
|
|
eventPath = os.path.join(yearPath, str(eventNumber))
|
|
|
|
if not os.path.exists(eventPath):
|
|
|
|
os.mkdir(eventPath)
|
2015-09-17 20:29:05 +02:00
|
|
|
|
|
|
|
|
2015-09-20 11:33:54 +02:00
|
|
|
def writeImages(images, destinationRoot):
|
2015-09-18 20:03:07 +02:00
|
|
|
sortedImages = sorted(images)
|
|
|
|
previousTime = None
|
|
|
|
eventNumber = 0
|
2015-09-17 20:29:05 +02:00
|
|
|
|
2015-09-18 20:03:07 +02:00
|
|
|
for imageTuple in sortedImages:
|
|
|
|
t = time.gmtime(imageTuple[0])
|
|
|
|
year = time.strftime("%Y", t)
|
|
|
|
if (previousTime == None) or ((previousTime + minEventDelta) < imageTuple[0]):
|
|
|
|
previousTime = imageTuple[0]
|
|
|
|
eventNumber = eventNumber + 1
|
|
|
|
createNewFolder(destinationRoot, year, eventNumber)
|
|
|
|
|
|
|
|
previousTime = imageTuple[0]
|
|
|
|
|
|
|
|
destination = os.path.join(destinationRoot, year, str(eventNumber))
|
|
|
|
shutil.copy(imageTuple[1], destination)
|
2015-09-17 20:29:05 +02:00
|
|
|
|
|
|
|
|
2015-09-20 11:33:54 +02:00
|
|
|
images = []
|
2015-06-04 04:28:47 +02:00
|
|
|
while not os.path.exists(source):
|
|
|
|
source = raw_input('Enter a valid source directory\n')
|
|
|
|
while not os.path.exists(destination):
|
|
|
|
destination = raw_input('Enter a valid destination directory\n')
|
|
|
|
|
|
|
|
for root, dirs, files in os.walk(source, topdown=False):
|
|
|
|
for file in files:
|
|
|
|
extension = os.path.splitext(file)[1][1:].upper()
|
2015-09-17 20:29:05 +02:00
|
|
|
path = os.path.join(root,file)
|
|
|
|
|
|
|
|
destinationPath = os.path.join(destination, extension)
|
2015-06-05 01:29:17 +02:00
|
|
|
|
|
|
|
if not os.path.exists(destinationPath):
|
2015-06-04 04:28:47 +02:00
|
|
|
os.mkdir(destinationPath)
|
2015-06-05 01:29:17 +02:00
|
|
|
|
2015-09-17 20:29:05 +02:00
|
|
|
if extension == "JPG":
|
2015-09-20 11:33:54 +02:00
|
|
|
postprocessImage(root, destinationPath, file)
|
2015-06-05 01:29:17 +02:00
|
|
|
else:
|
2015-09-17 20:29:05 +02:00
|
|
|
if os.path.exists(os.path.join(destinationPath,file)):
|
|
|
|
print("WARNING: this file was not copied :" + os.path.join(root,file))
|
|
|
|
shutil.copy(os.path.join(root,file), os.path.join(destination, extension, str(time.time()) + file))
|
|
|
|
else:
|
2015-09-18 20:03:07 +02:00
|
|
|
shutil.copy(os.path.join(root,file), destinationPath)
|
|
|
|
|
2015-09-20 11:33:54 +02:00
|
|
|
writeImages(os.path.join(destination, "JPG"))
|