Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'A Python script to organize digital photos' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A Python script to organize digital photos
Authored by: quentinsf on Apr 02, '03 05:41:30PM
For what it's worth, here's my version, which I've been using for some time, on Linux, then Windows, and now on my Mac, where I run it using an AppleScript menu item after Image Capture has put my photos in my Pictures directory.

This creates one directory per month and names the images within those based on their creation time, eg:
2003_03/2003_03_26-03_55_38.jpg
This means that I never lose the timestamp, even if I later manipulate the image using something which doesn't preserve times, tags etc.

Quentin


#!/usr/bin/python
# Copy image files from a source into a photo directory.
# Name them according to their date of creation.
# Put them in a directory based on the month.

import sys, os, glob, time, string

def copyfiles(srcdir, destdir, pattern, move=0):
    srcfiles = glob.glob(os.path.join(srcdir, pattern))
    for i in srcfiles:
        ext = os.path.splitext(i)[1]
        t = time.gmtime(os.path.getmtime(i))
        newname = time.strftime("%Y_%m_%d-%H_%M_%S",t) + ext
        monthdir = os.path.join(destdir, newname[:7])
        if not os.path.isdir(monthdir):
            print "Creating new dir", monthdir
            os.mkdir(monthdir)
        fullnewname = string.lower(os.path.join(monthdir,newname))

        # if file already exists, we add a suffix
        testname = fullnewname
        suffnum = 1
        while os.path.exists(testname):
                testname = "%s.%02d" % (fullnewname, suffnum)
                suffnum += 1
        if testname != fullnewname:
                fullnewname = testname

        print i,"->",fullnewname
        if move:
                os.rename(i, fullnewname)
        else:
                s = open(i,"rb")
                d = open(fullnewname, "wb")
                while 1:
                    b = s.read()
                    if not b: break
                    d.write(b)
                d.close()
                s.close()
    
def main():
    srcdir = "/Users/qsf/Pictures"
    destdir = "/Users/qsf/Pictures"
    if len(sys.argv) > 1:
        srcdir = sys.argv[1]
    if len(sys.argv) > 2:
        destdir = sys.argv[2]
    copyfiles(srcdir, destdir, "*.jpg*", move=1)
    copyfiles(srcdir, destdir, "*.JPG*", move=1)


if __name__ == "__main__":
    main()


[ Reply to This | # ]
A Python script to organize digital photos
Authored by: dimus on Apr 02, '03 10:13:59PM

I have trouble remembering what happened during this or that month, so I found that keeping all photos in one directory is better for me. Usually I organize a new directory when I have enough photos for a CD.



[ Reply to This | # ]
One more alternative - jhead
Authored by: haighy on Apr 03, '03 02:26:11AM

I use a program called jhead to rename my photos. It's written in C, so it's very fast...the link above has a pre-compiled OS X binary, but I built it from source with no problems.

The nice thing about this program is that it extracts image info from the EXIF header, so it's completely independent of the file's creation or modification time. Furthermore, it can reset the modification time of the actual JPEG to match that given in the EXIF metadata (in case you've rotated the pic in Photoshop, for example).

This one-liner renames an image in the form YYYY-MM-DD_hh-mm-ss, so that 'View by name' will list all files in chronological order, accurate to the second:

jhead -nf%Y-%m-%d_%H-%M-%S [files...]

Now cataloging those lurching, blurred, drunken mugshots is a breeze.



[ Reply to This | # ]
One more alternative - jhead
Authored by: dimus on Apr 03, '03 11:35:03AM

This is a great idea to use exif data for the date.
I'll try to figure out how to parse out the date from exif. If i'll succeed I'll post an updated script.



[ Reply to This | # ]