Advertisement
Guest User

Untitled

a guest
Jun 14th, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. import exifread
  2. import os
  3. import sys
  4. import logging
  5. import shutil
  6.  
  7. logging.basicConfig()
  8.  
  9.  
  10. def get_filepaths(directory):
  11.     file_paths = []
  12.     for root, directories, files in os.walk(directory):
  13.         for filename in files:
  14.             filepath = os.path.join(root, filename)
  15.             file_paths.append(filepath)
  16.     return file_paths  
  17.  
  18.  
  19. def process_fotos():
  20.     input_dir = sys.argv[1]
  21.     output_dir = sys.argv[2]
  22.     input_files = get_filepaths(input_dir)
  23.     print('Input dir - %s, output - %s' % (input_dir, output_dir))
  24.     print('Found %d input files' % len(input_files))
  25.     exifs = {}
  26.     for fi in input_files:
  27.         f = open(fi, 'rb')
  28.         tags = exifread.process_file(f)
  29.         exifs[fi] = str(tags.get('EXIF DateTimeOriginal', ''))
  30.     months = {}    
  31.     for f, dat in exifs.items():
  32.         if len(dat):
  33.             year_month = '.'.join(dat.split()[0].split(':')[0:2])
  34.             months.setdefault(year_month, []).append(f)
  35.         else:
  36.             print("No EXIF %s" % f)
  37.     n = 0
  38.     for month, files in months.items():
  39.         directory = os.path.join(output_dir, month)
  40.         if not os.path.exists(directory):
  41.             os.makedirs(directory)
  42.         for f in files:
  43.             shutil.copy(f, directory)
  44.             n += 1
  45.     print('Done %d files' % n)
  46.  
  47.  
  48. def main():
  49.     process_fotos()
  50.  
  51. if __name__ == "__main__":
  52.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement