elleryq
1/2/2014 - 9:27 AM

According the photo's taken date to classify photos like this: your_path/2014/1/1 You have to install exifread: pip install exifread

According the photo's taken date to classify photos like this: your_path/2014/1/1 You have to install exifread: pip install exifread

from __future__ import print_function
import sys
import os
import exifread
import glob
from datetime import datetime
import shutil


def main():
    if len(sys.argv)<2:
        print("Need arguments")
        print("For example: *.jpg d:\\tmp")
        sys.exit(-1)
        
    jpgs = glob.glob(sys.argv[1])
    outputBase = sys.argv[2]
    print(outputBase)

    for jpg in jpgs:
        print(jpg)
        tags = exifread.process_file(open(jpg, "rb"))
        if not tags:
            continue
        takenDate = tags['EXIF DateTimeOriginal']
        dt = datetime.strptime(str(takenDate), "%Y:%m:%d %H:%M:%S")
        destPath = os.path.join(outputBase, str(dt.year),
                                str(dt.month), str(dt.day))
        if not os.path.exists(destPath):
            os.makedirs(destPath)
        shutil.copy(jpg, destPath)
    
if __name__ == "__main__":
    main()