Skip to content

Instantly share code, notes, and snippets.

@dimnikolos
Forked from nag4/image_to_dir_by_date.py
Last active October 12, 2016 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimnikolos/83778cbb7bc1b7e4146a4347d01bf1dd to your computer and use it in GitHub Desktop.
Save dimnikolos/83778cbb7bc1b7e4146a4347d01bf1dd 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(os.path.join(root, 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, str(yyyy) + str(mm) + str(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(os.path.join(root,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