Tuesday, 2 November 2010

Simple way to organize photos by date

I have a lot of pictures. Like most people today. They were all dumped in one directory. It was very difficult to view them. So I wrote a very short python script that reads from the EXIF date the photo was taken. Create a directory for that date and move the photo in this directory.
Here's the script:
import os
from PIL import Image
import shutil

cdate_tag = 0x9003

files = [f for f in os.listdir('.') if f.endswith('.JPG')]

for f in files:
    if os.path.isfile(f):
        img = Image.open(f)
        date = img._getexif()[0x9003].split(' ')[0].replace(':','_')
        if not os.path.isdir(date):
            os.mkdir(date)
        shutil.move(f,os.path.join(date,f))
        print f,date
        img.fp.close()

No comments:

Post a Comment