Skip to content

Instantly share code, notes, and snippets.

@nag4
Created May 2, 2014 11:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nag4/ccea3b87bce3d2495bd2 to your computer and use it in GitHub Desktop.
Save nag4/ccea3b87bce3d2495bd2 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from PIL import Image
import os
import shutil
# exists src_dir/hoge.jpg, fuga.png, etc...
src_dir = "/Users/name/hoge/"
# create dst_dir/yyyymmdd/
dst_dir = "/Users/name/fuga/"
if os.path.exists(dst_dir) == False:
os.mkdir(dst_dir)
for root, dirs, files in os.walk(src_dir):
for filename in files:
try:
image = Image.open(src_dir + filename)
# 36867 : EXIF DateTimeOriginal
date = image._getexif()[36867]
yyyy, mm, dd = date[:4], date[5:7], date[8:10]
yyyymmdd_dir = os.path.join(dst_dir, yyyy + mm + dd)
if os.path.exists(yyyymmdd_dir) == False:
os.mkdir(yyyymmdd_dir)
dst = os.path.join(yyyymmdd_dir, filename)
if os.path.exists(dst) == False:
shutil.copy2(src_dir + filename, dst)
except Exception as e:
# .DS_Store must Die
print filename + ' is fail.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment