Advertisement
Guest User

Untitled

a guest
Jun 9th, 2016
1,642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.57 KB | None | 0 0
  1. # Author: Guillaume Lambany
  2. # Email: GLAMBANY@GMAIL.COM
  3. # Date: 2016-06-09
  4. # Final format needed:  2016\05\2016_05_29\20160529223016_0001.jpg
  5. #                       YYYY\MM\YYYY_MM_DD\YYYYMMDDHHMMSS_COUNT.jpg
  6. # This script needs exiftool.exe to work properly
  7. # 'SRCPATH' and 'EXIFTOOL' constants needs to be set properly
  8. # very important: rename exiftool(-k).exe to exiftool.exe
  9.  
  10. #import dependencies
  11. import subprocess, sys, json
  12. from os import listdir, rename, path, makedirs
  13.  
  14. #Total Character size of the incrementing number, at the end of the destination filename.
  15. ZEROPAD = 4
  16.  
  17. #Source Path to process. Don't forget the "/" at the end
  18. SRCPATH = 'C:/Photos/'
  19.  
  20. #EXIFTOOL location
  21. EXIFTOOL = 'C:/apps_local/exiftool.exe'
  22.  
  23. #function that return a list of files in a given path
  24. def list_files(pathname):
  25.     # return empty array if folder doesn't exists
  26.     if not path.exists(pathname):
  27.         return []
  28.  
  29.     # returns a list of names (with extension, without full path) of all files in folder path
  30.     files = []
  31.     for name in listdir(pathname):
  32.         if path.isfile(path.join(pathname, name)):
  33.             files.append(name)
  34.     return files
  35.  
  36. # Test if the source directory exists
  37. if not path.exists(SRCPATH):
  38.     print 'No such Path exists: ' + SRCPATH
  39.     quit()
  40.  
  41. #Query exiftool for metadata
  42. 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)
  43. exifTl_outRaw = exifTl_Proc.communicate()[0]
  44. exifTl_RetCode = exifTl_Proc.returncode
  45. exifTl_Proc.terminate()
  46.  
  47. #debug
  48. #print exifTl_outRaw
  49.  
  50. #Verify if exiftool ran properly
  51. if exifTl_RetCode == 0 and len(exifTl_outRaw) > 0:
  52.     exifTl_outJson = json.loads(exifTl_outRaw)
  53. else:
  54.     print 'Error running EXIFTOOL. ReturnCode = ' + str(exifTl_RetCode)
  55.     quit()
  56.  
  57. # Run through all the output data from exiftool
  58. for exifTl_Dict in exifTl_outJson:
  59.     sourceFile = exifTl_Dict['SourceFile']
  60.     if 'DateTimeOriginal' in exifTl_Dict.keys():
  61.         imgDateTimeRaw = exifTl_Dict['DateTimeOriginal']
  62.     elif 'CreateDate' in exifTl_Dict.keys():
  63.         imgDateTimeRaw = exifTl_Dict['CreateDate']
  64.     else:
  65.         #error, tags not found
  66.         print 'Metadata tags not found (CreateDate or DateTimeOriginal) in file: ' + sourceFile
  67.         continue
  68.  
  69.     #debug
  70.     #print imgDateTimeRaw
  71.  
  72.     imgDate = imgDateTimeRaw.split(' ')[0].split(':')
  73.     imgTime = imgDateTimeRaw.split(' ')[1].split(':')
  74.  
  75.     #debug
  76.     #print sourceFile + ' DATE:' + '-'.join(imgDate) + ' TIME:' + ':'.join(imgTime)
  77.  
  78.     #Generate destination path
  79.     dstPath = (SRCPATH +
  80.                 imgDate[0] + '/' + imgDate[1] + '/' +
  81.                 '_'.join(imgDate) + '/')
  82.  
  83.     # create the new folder if it doesn't already exists
  84.     if not path.exists(dstPath):
  85.         makedirs(dstPath)
  86.         nbrFiles = 0
  87.     else:
  88.         # check number of files in the destination directory
  89.         nbrFiles = len(list_files(SRCPATH + imgDate[0] + '/' + imgDate[1] + '/' + '_'.join(imgDate) + '/'))
  90.  
  91.     # the increment number that will be used in the final filename, starts at the number of files in the directory + 1
  92.     countNumber = nbrFiles + 1
  93.  
  94.     # Increment the increment number until an unused filename is found
  95.     while True:
  96.         dstFile = (''.join(imgDate) +                           #YYYYMMDD
  97.                     ''.join(imgTime) +                          #HHMMSS
  98.                     "_{0:0>{1}}".format(countNumber, ZEROPAD) + #Increment number
  99.                     path.splitext(sourceFile)[1])               #Extention
  100.  
  101.         #debug
  102.         #print 'path+file::' + dstPath + '|' + dstFile
  103.  
  104.         #exit the loop if the new file name doesn't already exists
  105.         if not path.isfile(dstPath + dstFile):
  106.             break
  107.         countNumber=+1
  108.  
  109.     # Move file to new folder
  110.     rename(sourceFile, dstPath +  dstFile)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement