Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Author: Guillaume Lambany
- # Email: GLAMBANY@GMAIL.COM
- # Date: 2016-06-09
- # Final format needed: 2016\05\2016_05_29\20160529223016_0001.jpg
- # YYYY\MM\YYYY_MM_DD\YYYYMMDDHHMMSS_COUNT.jpg
- # This script needs exiftool.exe to work properly
- # 'SRCPATH' and 'EXIFTOOL' constants needs to be set properly
- # very important: rename exiftool(-k).exe to exiftool.exe
- #import dependencies
- import subprocess, sys, json
- from os import listdir, rename, path, makedirs
- #Total Character size of the incrementing number, at the end of the destination filename.
- ZEROPAD = 4
- #Source Path to process. Don't forget the "/" at the end
- SRCPATH = 'C:/Photos/'
- #EXIFTOOL location
- EXIFTOOL = 'C:/apps_local/exiftool.exe'
- #function that return a list of files in a given path
- def list_files(pathname):
- # return empty array if folder doesn't exists
- if not path.exists(pathname):
- return []
- # returns a list of names (with extension, without full path) of all files in folder path
- files = []
- for name in listdir(pathname):
- if path.isfile(path.join(pathname, name)):
- files.append(name)
- return files
- # Test if the source directory exists
- if not path.exists(SRCPATH):
- print 'No such Path exists: ' + SRCPATH
- quit()
- #Query exiftool for metadata
- exifTl_Proc = subprocess.Popen(EXIFTOOL + ' -fast -json -DateTimeOriginal -CreateDate -ext jpg -ext jpeg -ext mp4 -ext mp4 ' + SRCPATH, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=False)
- exifTl_outRaw = exifTl_Proc.communicate()[0]
- exifTl_RetCode = exifTl_Proc.returncode
- exifTl_Proc.terminate()
- #debug
- #print exifTl_outRaw
- #Verify if exiftool ran properly
- if exifTl_RetCode == 0 and len(exifTl_outRaw) > 0:
- exifTl_outJson = json.loads(exifTl_outRaw)
- else:
- print 'Error running EXIFTOOL. ReturnCode = ' + str(exifTl_RetCode)
- quit()
- # Run through all the output data from exiftool
- for exifTl_Dict in exifTl_outJson:
- sourceFile = exifTl_Dict['SourceFile']
- if 'DateTimeOriginal' in exifTl_Dict.keys():
- imgDateTimeRaw = exifTl_Dict['DateTimeOriginal']
- elif 'CreateDate' in exifTl_Dict.keys():
- imgDateTimeRaw = exifTl_Dict['CreateDate']
- else:
- #error, tags not found
- print 'Metadata tags not found (CreateDate or DateTimeOriginal) in file: ' + sourceFile
- continue
- #debug
- #print imgDateTimeRaw
- imgDate = imgDateTimeRaw.split(' ')[0].split(':')
- imgTime = imgDateTimeRaw.split(' ')[1].split(':')
- #debug
- #print sourceFile + ' DATE:' + '-'.join(imgDate) + ' TIME:' + ':'.join(imgTime)
- #Generate destination path
- dstPath = (SRCPATH +
- imgDate[0] + '/' + imgDate[1] + '/' +
- '_'.join(imgDate) + '/')
- # create the new folder if it doesn't already exists
- if not path.exists(dstPath):
- makedirs(dstPath)
- nbrFiles = 0
- else:
- # check number of files in the destination directory
- nbrFiles = len(list_files(SRCPATH + imgDate[0] + '/' + imgDate[1] + '/' + '_'.join(imgDate) + '/'))
- # the increment number that will be used in the final filename, starts at the number of files in the directory + 1
- countNumber = nbrFiles + 1
- # Increment the increment number until an unused filename is found
- while True:
- dstFile = (''.join(imgDate) + #YYYYMMDD
- ''.join(imgTime) + #HHMMSS
- "_{0:0>{1}}".format(countNumber, ZEROPAD) + #Increment number
- path.splitext(sourceFile)[1]) #Extention
- #debug
- #print 'path+file::' + dstPath + '|' + dstFile
- #exit the loop if the new file name doesn't already exists
- if not path.isfile(dstPath + dstFile):
- break
- countNumber=+1
- # Move file to new folder
- rename(sourceFile, dstPath + dstFile)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement