Skip to content

Instantly share code, notes, and snippets.

@shouth
Last active November 14, 2018 18:19
Show Gist options
  • Save shouth/9313494e59a76c0b1c47b387260987f1 to your computer and use it in GitHub Desktop.
Save shouth/9313494e59a76c0b1c47b387260987f1 to your computer and use it in GitHub Desktop.
from PIL import Image
from datetime import datetime
from pathlib import Path
import imghdr
import os
import shutil
for path in Path.cwd().iterdir():
if Path.is_dir(path) or imghdr.what(path) != 'jpeg': continue
exif = Image.open(path)._getexif()
if not 0x9003 in exif: continue
img_datetime = datetime.strptime(exif[0x9003], '%Y:%m:%d %H:%M:%S')
dest = Path.cwd() / str(img_datetime.year) / str(img_datetime.month).zfill(2)
if not dest.exists(): os.makedirs(dest)
shutil.move(str(path), dest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment