Created
February 14, 2013 19:14
-
-
Save alexwright/4955446 to your computer and use it in GitHub Desktop.
Quick and dirty script to sort pictures into a directory structure based on EXIF DateTaken date/times.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import EXIF | |
import datetime | |
work_dir = '../pics' | |
exif_date_format = '%Y:%m:%d %H:%M:%S' | |
for dirname, dirnames, filenames in os.walk(work_dir): | |
for filename in filenames: | |
old_file_path = os.path.join(work_dir, filename) | |
file = open(old_file_path, 'rb') | |
tags = EXIF.process_file(file, stop_tag='Image DateTime') | |
if 'Image DateTime' in tags: | |
dt = datetime.datetime.strptime(tags['Image DateTime'].values, exif_date_format) | |
dir = dt.strftime('path/%Y/%m/%Y-%m-%d/') | |
if not os.path.exists(dir): | |
os.makedirs(dir) | |
new_file_path = os.path.join(dir, filename) | |
os.rename(old_file_path, new_file_path) | |
file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Badly named variables and posiable exceptions on the makedirs, and I'm sure other places. But it does at least sort out my DCIM dir.
Uses exif-py.