-
-
Save alexwlchan/8280162 to your computer and use it in GitHub Desktop.
My fork of @cliss’s photo management script. Tweaked to: * Get date from `os.path.getmtime(f)` (lifted from @jdmonaco’s fork)
* Sort any format with a three-character extension (currently .JPG, .TIF, .PNG and .MOV). Edit the list in line 40 for more formats. It also normalises extensions to lowercase.
* Tweaked some bits of the code
* Moves file…
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 | |
import sys | |
import os, shutil | |
import subprocess | |
import os.path | |
from datetime import datetime | |
######################## Functions ######################### | |
def photoDate(f): | |
"Return the date/time on which the given photo was taken." | |
try: | |
cDate = subprocess.check_output(['sips', '-g', 'creation', f]) | |
cDate = cDate.split('\n')[1].lstrip().split(': ')[1] | |
return datetime.strptime(cDate, "%Y:%m:%d %H:%M:%S") | |
except: | |
print 'sips failed... falling back to modified time' | |
return datetime.fromtimestamp(os.path.getmtime(f)) | |
###################### Main program ######################## | |
# Where the photos are and where they're going. | |
sourceDir = os.environ['HOME'] + '/Pictures/iPhone Incoming' | |
destDir = os.environ['HOME'] + '/Pictures/iPhone' | |
errorDir = destDir + '/Unsorted/' | |
# The format for the new file names. | |
fmt = "%Y-%m-%d %H-%M-%S" | |
# The problem files. | |
problems = [] | |
# Get all the images in the source folder. | |
files = os.listdir(sourceDir) | |
photos = [] | |
for x in files: | |
if x[-4:].lower() in ['.jpg', '.tif', '.png', '.mov']: | |
photos.append(x) | |
# Prepare to output as processing occurs | |
lastMonth = 0 | |
lastYear = 0 | |
# Create the destination folder if necessary | |
for newDir in [destDir, errorDir]: | |
if not os.path.exists(newDir): | |
os.makedirs(newDir) | |
# Copy photos into year and month subfolders. Name the copies according to | |
# their timestamps. If more than one photo has the same timestamp, add | |
# suffixes 'a', 'b', etc. to the names. | |
for photo in photos: | |
# print "Processing %s..." % photo | |
original = sourceDir + '/' + photo | |
suffix = 'a' | |
fileExt = photo[-4:].lower() | |
try: | |
pDate = photoDate(original) | |
yr = pDate.year | |
mo = pDate.month | |
if not lastYear == yr or not lastMonth == mo: | |
sys.stdout.write('\nProcessing %04d-%02d...' % (yr, mo)) | |
lastMonth = mo | |
lastYear = yr | |
else: | |
sys.stdout.write('.') | |
newname = pDate.strftime(fmt) | |
thisDestDir = destDir + '/%04d/%02d' % (yr, mo) | |
if not os.path.exists(thisDestDir): | |
os.makedirs(thisDestDir) | |
duplicate = thisDestDir + '/%s' % (newname) + fileExt | |
while os.path.exists(duplicate): | |
newname = pDate.strftime(fmt) + suffix | |
duplicate = destDir + '/%04d/%02d/%s' % (yr, mo, newname) + fileExt | |
suffix = chr(ord(suffix) + 1) | |
shutil.move(original, duplicate) | |
except Exception: | |
shutil.move(original, errorDir + photo) | |
problems.append(photo) | |
except: | |
sys.exit("Execution stopped.") | |
# Report the problem files, if any. | |
if len(problems) > 0: | |
print "\nProblem files:" | |
print "\n".join(problems) | |
print "These can be found in: %s" % errorDir |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment