used file counter for file names, one method to be called after images have been copied
This commit is contained in:
parent
4dc8054890
commit
71791236b9
64
jpgSorter.py
64
jpgSorter.py
|
@ -1,9 +1,10 @@
|
||||||
import os.path
|
import os.path
|
||||||
import exifread
|
import exifread
|
||||||
import time
|
from time import localtime, strftime, strptime, mktime
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
minEventDelta = 60 * 60 * 24 * 4 # 4 days in seconds
|
minEventDelta = 60 * 60 * 24 * 4 # 4 days in seconds
|
||||||
|
unknownDateFolderName = "Datum unbekannt"
|
||||||
|
|
||||||
def getMinimumCreationTime(exif_data):
|
def getMinimumCreationTime(exif_data):
|
||||||
creationTime = None
|
creationTime = None
|
||||||
|
@ -27,22 +28,27 @@ def getMinimumCreationTime(exif_data):
|
||||||
|
|
||||||
return creationTime
|
return creationTime
|
||||||
|
|
||||||
def postprocessImage(images, sourceDir, imageDirectory, fileName):
|
def postprocessImage(images, imageDirectory, fileName):
|
||||||
imagePath = os.path.join(sourceDir, fileName)
|
imagePath = os.path.join(imageDirectory, fileName)
|
||||||
image = open(imagePath, 'rb')
|
image = open(imagePath, 'rb')
|
||||||
|
creationTime = None
|
||||||
try:
|
try:
|
||||||
exifTags = exifread.process_file(image, details=False)
|
exifTags = exifread.process_file(image, details=False)
|
||||||
|
creationTime = getMinimumCreationTime(exifTags)
|
||||||
except:
|
except:
|
||||||
print("invalid exif tags for " + fileName)
|
print("invalid exif tags for " + fileName)
|
||||||
creationTime = getMinimumCreationTime(exifTags)
|
|
||||||
|
|
||||||
# distinct different time types
|
# distinct different time types
|
||||||
if creationTime is None:
|
if creationTime is None:
|
||||||
creationTime = time.gmtime(os.path.getctime(imagePath))
|
creationTime = localtime(os.path.getctime(imagePath))
|
||||||
else:
|
else:
|
||||||
creationTime = time.strptime(str(creationTime), "%Y:%m:%d %H:%M:%S")
|
try:
|
||||||
|
creationTime = strptime(str(creationTime), "%Y:%m:%d %H:%M:%S")
|
||||||
|
except:
|
||||||
|
creationTime = localtime(os.path.getctime(imagePath))
|
||||||
|
|
||||||
images.append((time.mktime(creationTime), imagePath))
|
images.append((mktime(creationTime), imagePath))
|
||||||
|
image.close()
|
||||||
|
|
||||||
|
|
||||||
def createNewFolder(destinationRoot, year, eventNumber):
|
def createNewFolder(destinationRoot, year, eventNumber):
|
||||||
|
@ -53,21 +59,47 @@ def createNewFolder(destinationRoot, year, eventNumber):
|
||||||
if not os.path.exists(eventPath):
|
if not os.path.exists(eventPath):
|
||||||
os.mkdir(eventPath)
|
os.mkdir(eventPath)
|
||||||
|
|
||||||
|
def createUnknownDateFolder(destinationRoot):
|
||||||
|
path = os.path.join(destinationRoot, unknownDateFolderName)
|
||||||
|
if not os.path.exists(path):
|
||||||
|
os.mkdir(path)
|
||||||
|
|
||||||
|
|
||||||
def writeImages(images, destinationRoot):
|
def writeImages(images, destinationRoot):
|
||||||
sortedImages = sorted(images)
|
sortedImages = sorted(images)
|
||||||
previousTime = None
|
previousTime = None
|
||||||
eventNumber = 0
|
eventNumber = 0
|
||||||
|
today = strftime("%d/%m/%Y")
|
||||||
|
|
||||||
for imageTuple in sortedImages:
|
for imageTuple in sortedImages:
|
||||||
t = time.gmtime(imageTuple[0])
|
t = localtime(imageTuple[0])
|
||||||
year = time.strftime("%Y", t)
|
year = strftime("%Y", t)
|
||||||
if (previousTime == None) or ((previousTime + minEventDelta) < imageTuple[0]):
|
creationDate = strftime("%d/%m/%Y", t)
|
||||||
|
if(creationDate == today):
|
||||||
|
createUnknownDateFolder(destinationRoot)
|
||||||
|
destination = os.path.join(destinationRoot, unknownDateFolderName)
|
||||||
|
shutil.move(imageTuple[1], destination)
|
||||||
|
else:
|
||||||
|
if (previousTime == None) or ((previousTime + minEventDelta) < imageTuple[0]):
|
||||||
|
previousTime = imageTuple[0]
|
||||||
|
eventNumber = eventNumber + 1
|
||||||
|
createNewFolder(destinationRoot, year, eventNumber)
|
||||||
|
|
||||||
previousTime = imageTuple[0]
|
previousTime = imageTuple[0]
|
||||||
eventNumber = eventNumber + 1
|
|
||||||
createNewFolder(destinationRoot, year, eventNumber)
|
|
||||||
|
|
||||||
previousTime = imageTuple[0]
|
|
||||||
|
|
||||||
destination = os.path.join(destinationRoot, year, str(eventNumber))
|
destination = os.path.join(destinationRoot, year, str(eventNumber))
|
||||||
shutil.copy(imageTuple[1], destination)
|
# 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)):
|
||||||
|
destination = os.path.join(destinationRoot, year-1, str(eventNumber))
|
||||||
|
|
||||||
|
shutil.move(imageTuple[1], destination)
|
||||||
|
|
||||||
|
|
||||||
|
def postprocessImages(imageDirectory):
|
||||||
|
images = []
|
||||||
|
for root, dirs, files in os.walk(imageDirectory):
|
||||||
|
for file in files:
|
||||||
|
postprocessImage(images, imageDirectory, file)
|
||||||
|
|
||||||
|
writeImages(images, imageDirectory)
|
||||||
|
|
Loading…
Reference in New Issue