Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import exifread
- import os
- import sys
- import logging
- import shutil
- logging.basicConfig()
- def get_filepaths(directory):
- file_paths = []
- for root, directories, files in os.walk(directory):
- for filename in files:
- filepath = os.path.join(root, filename)
- file_paths.append(filepath)
- return file_paths
- def process_fotos():
- input_dir = sys.argv[1]
- output_dir = sys.argv[2]
- input_files = get_filepaths(input_dir)
- print('Input dir - %s, output - %s' % (input_dir, output_dir))
- print('Found %d input files' % len(input_files))
- exifs = {}
- for fi in input_files:
- f = open(fi, 'rb')
- tags = exifread.process_file(f)
- exifs[fi] = str(tags.get('EXIF DateTimeOriginal', ''))
- months = {}
- for f, dat in exifs.items():
- if len(dat):
- year_month = '.'.join(dat.split()[0].split(':')[0:2])
- months.setdefault(year_month, []).append(f)
- else:
- print("No EXIF %s" % f)
- n = 0
- for month, files in months.items():
- directory = os.path.join(output_dir, month)
- if not os.path.exists(directory):
- os.makedirs(directory)
- for f in files:
- shutil.copy(f, directory)
- n += 1
- print('Done %d files' % n)
- def main():
- process_fotos()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement