From 38ee7077a47a96dd551b14ec7b8c2aa85081ce8b Mon Sep 17 00:00:00 2001 From: Lukas Hahmann Date: Fri, 18 Sep 2015 20:03:07 +0200 Subject: [PATCH] added a mechanism to distinguis pictures from different event. Thereby an event is divided from another one by a time span of 4 days --- recovery.py | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/recovery.py b/recovery.py index 95e572e..31f6c85 100644 --- a/recovery.py +++ b/recovery.py @@ -8,6 +8,8 @@ import exifread source = sys.argv[1] destination = sys.argv[2] +images = [] +minEventDelta = 60 * 60 * 24 * 4 # 4 days in seconds def get_minimum_creation_time(exif_data): @@ -38,20 +40,35 @@ def postprocess_image(sourceDir, imageDirectory, fileName): print time.asctime(creationTime) - year = time.strftime("%Y", creationTime) - month = time.strftime("%m") + images.append((time.mktime(creationTime), imagePath)) - yearPath = os.path.join(imageDirectory, year) +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) - monthPath = os.path.join(yearPath, month) - if not os.path.exists(monthPath): - os.mkdir(monthPath) +def write_images(destinationRoot): + sortedImages = sorted(images) + previousTime = None + eventNumber = 0 - shutil.copy(imagePath, os.path.join(monthPath, file)) + 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) while not os.path.exists(source): @@ -71,10 +88,11 @@ for root, dirs, files in os.walk(source, topdown=False): if extension == "JPG": postprocess_image(root, destinationPath, file) - print "sorted jpg" else: 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: - shutil.copy(os.path.join(root,file), destinationPath) \ No newline at end of file + shutil.copy(os.path.join(root,file), destinationPath) + +write_images(os.path.join(destination, "JPG")) \ No newline at end of file