Skip to content

Instantly share code, notes, and snippets.

@atomAltera
Last active September 13, 2016 03:38
Show Gist options
  • Save atomAltera/2d47e23be15646ffbf6e70fabd6594f4 to your computer and use it in GitHub Desktop.
Save atomAltera/2d47e23be15646ffbf6e70fabd6594f4 to your computer and use it in GitHub Desktop.
Python script for sorting pictures based on EXIF date
import glob
from datetime import datetime
from os import makedirs
from shutil import copy
import exifread
from path import path
def process_file(file_path, dest_dir):
file_path = path(file_path)
dest_dir = path(dest_dir)
with open(file_path, 'rb') as f:
tags = exifread.process_file(f)
if not ('Image DateTime' in tags.keys()):
print("Err, file {0} has no EXIF".format(file_path))
return
date_time_str = str(tags['Image DateTime'])
date_time = datetime.strptime(date_time_str, '%Y:%m:%d %H:%M:%S')
date = date_time.date()
dest_dir = path.joinpath(dest_dir, str(date.year), str(date.month), str(date.day))
makedirs(dest_dir, exist_ok=True)
dest_path = path.joinpath(dest_dir, file_path.name)
print('Copying {0} to {1}'.format(file_path, dest_path))
copy(file_path, dest_path)
for file_path in glob.iglob('/home/altera/recover/*/*.jpg'):
try:
process_file(file_path, '/home/altera/recovered/images/')
except Exception as e:
print(e)
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment