55import shutil
66
77minEventDelta = 60 * 60 * 24 * 4 # 4 days in seconds
8- unknownDateFolderName = "Datum unbekannt "
8+ unknownDateFolderName = "date-unknown "
99
1010def getMinimumCreationTime (exif_data ):
1111 creationTime = None
@@ -51,32 +51,37 @@ def postprocessImage(images, imageDirectory, fileName):
5151 images .append ((mktime (creationTime ), imagePath ))
5252 image .close ()
5353
54+ # Creates the requested path recursively.
55+ def createPath (newPath ):
56+ if not os .path .exists (newPath ):
57+ os .makedirs (newPath )
5458
55- def createNewFolder (destinationRoot , year , eventNumber ):
56- yearPath = os .path .join (destinationRoot , year )
57- if not os .path .exists (yearPath ):
58- os .mkdir (yearPath )
59- eventPath = os .path .join (yearPath , str (eventNumber ))
60- if not os .path .exists (eventPath ):
61- os .mkdir (eventPath )
59+ # Pass None for month to create 'year/eventNumber' directories instead of 'year/month/eventNumber'.
60+ def createNewFolder (destinationRoot , year , month , eventNumber ):
61+ if month is not None :
62+ newPath = os .path .join (destinationRoot , year , month , str (eventNumber ))
63+ else :
64+ newPath = os .path .join (destinationRoot , year , str (eventNumber ))
65+
66+ createPath (newPath )
6267
6368def createUnknownDateFolder (destinationRoot ):
6469 path = os .path .join (destinationRoot , unknownDateFolderName )
65- if not os .path .exists (path ):
66- os .mkdir (path )
67-
70+ createPath (path )
6871
69- def writeImages (images , destinationRoot ):
72+ def writeImages (images , destinationRoot , splitByMonth = False ):
7073 sortedImages = sorted (images )
7174 previousTime = None
7275 eventNumber = 0
76+ previousDestination = None
7377 today = strftime ("%d/%m/%Y" )
7478
7579 for imageTuple in sortedImages :
7680 destination = ""
7781 destinationFilePath = ""
7882 t = localtime (imageTuple [0 ])
7983 year = strftime ("%Y" , t )
84+ month = splitByMonth and strftime ("%m" , t ) or None
8085 creationDate = strftime ("%d/%m/%Y" , t )
8186 fileName = ntpath .basename (imageTuple [1 ])
8287
@@ -87,18 +92,22 @@ def writeImages(images, destinationRoot):
8792
8893 else :
8994 if (previousTime == None ) or ((previousTime + minEventDelta ) < imageTuple [0 ]):
90- previousTime = imageTuple [0 ]
9195 eventNumber = eventNumber + 1
92- createNewFolder (destinationRoot , year , eventNumber )
93-
96+ createNewFolder (destinationRoot , year , month , eventNumber )
97+
9498 previousTime = imageTuple [0 ]
9599
96- destination = os .path .join (destinationRoot , year , str (eventNumber ))
97- # it may be possible that an event covers 2 years.
98- # in such a case put all the images to the even in the old year
100+ destComponents = [destinationRoot , year , month , str (eventNumber )]
101+ destComponents = [v for v in destComponents if v is not None ]
102+ destination = os .path .join (* destComponents )
103+
104+ # it may be possible that an event covers 2 years.
105+ # in such a case put all the images to the event in the old year
99106 if not (os .path .exists (destination )):
100- destination = os .path .join (destinationRoot , str (int (year ) - 1 ), str (eventNumber ))
107+ destination = previousDestination
108+ # destination = os.path.join(destinationRoot, str(int(year) - 1), str(eventNumber))
101109
110+ previousDestination = destination
102111 destinationFilePath = os .path .join (destination , fileName )
103112
104113 if not (os .path .exists (destinationFilePath )):
@@ -108,10 +117,10 @@ def writeImages(images, destinationRoot):
108117 os .remove (imageTuple [1 ])
109118
110119
111- def postprocessImages (imageDirectory ):
120+ def postprocessImages (imageDirectory , splitByMonth ):
112121 images = []
113122 for root , dirs , files in os .walk (imageDirectory ):
114123 for file in files :
115124 postprocessImage (images , imageDirectory , file )
116125
117- writeImages (images , imageDirectory )
126+ writeImages (images , imageDirectory , splitByMonth )
0 commit comments