Created
January 11, 2014 05:50
-
-
Save samstewart/8367560 to your computer and use it in GitHub Desktop.
Photo import script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # awesome python script that takes all files from the camera and archives them. | |
| # it imports from the temp directory and builds a large archive in the Photo folders. | |
| # The current iteration is quite functional in an effort for conciseness. | |
| import os, time, shutil, calendar, subprocess, sys, fnmatch, shutil, re | |
| from os import path | |
| class PhotoArchiver: | |
| PHOTOS_REPO = path.expanduser("/Volumes/Patroclus/Photos") | |
| PHOTOS_SRC = path.expanduser("~/Pictures") | |
| def dst(self, fname): | |
| """Constructs the special archive destination folder structure""" | |
| src = path.join(self.PHOTOS_SRC, fname) | |
| if not path.exists(src): | |
| return None | |
| # I use modification time because it is the most reliable. | |
| # There are better ways to get the creation time, but this is the simplest | |
| # and copy from camera makes modification time the same thing as creation time. | |
| creation_stamp = path.getmtime(src) | |
| time_struct = time.localtime(creation_stamp) | |
| date_path = '%04d/%s/%02d' % (time_struct[0], calendar.month_name[time_struct[1]], time_struct[2] ) | |
| dst = path.join(self.PHOTOS_REPO, date_path) | |
| # create intermediate directories | |
| if not path.exists(dst): | |
| print "Making directory: %s" % (dst, ) | |
| os.makedirs(dst) | |
| # now see if the file exists | |
| dst = path.join(dst, fname) | |
| # don't overwrite | |
| return (dst if not path.exists(dst) else None) | |
| def archive(self): | |
| """ | |
| Archives the photos in the PHOTO_SRC directory | |
| """ | |
| # start by looking only at images (more file types? like .MOV?) | |
| srcs = filter(re.compile("(.JPG|.MOV|.AVI)$", re.IGNORECASE).search, os.listdir(self.PHOTOS_SRC)) | |
| # now get the destination paths | |
| dsts = map(self.dst, srcs) | |
| # make sure all destination paths were properly created | |
| if not all(dsts): | |
| print "Error importing photos" | |
| # get rid of all the bad ones and continue | |
| dsts = filter(lambda d: d is not None, dsts) | |
| # moves the files | |
| map(lambda f: shutil.move(path.join(self.PHOTOS_SRC, f[0]), f[1]), zip(srcs, dsts)) | |
| print "Import done! Close this window" | |
| os.system("open -a picasa") | |
| PhotoArchiver().archive() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment