OS-X Importing pictures with Python script
Recently I said goodbye to Apple’s Aperture to manage my pictures, I switched to Adobe Bridge. The main reason for this is the huge database Aperture creates. For each picture it’ll make at least 7 extra files.
The one thing I missed from Aperture is now covered by this Python script: importing pictures into folders based on year/month of picture taken.
The script copies all new files from volume NIKON/DCIM into my user folder/Pictures/Fotos/year/month. If the file already exists it is skipped.
Source and destination are easily adjustable.
#!/usr/bin/python
import os, shutil
import subprocess
import os.path
from datetime import datetime
def photoDate(f):
“Return the date/time on which the given photo was taken.”
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”)
# Where the photos are and where they’re going.
sourceDir = ‘/Volumes/NIKON/DCIM’
destDir = os.environ[‘HOME’] + ‘/Pictures/Fotos’
folders = os.listdir(sourceDir)
for folder in folders:
workDir = sourceDir + ‘/’ + folder
print ‘Copying files from ‘ + workDir
photos = os.listdir(workDir)
photos = [ x for x in photos if x[-4:] == ‘.jpg’ or x[-4:] == ‘.JPG’ ]
for photo in photos:
original = workDir + ‘/’ + photo
#try:
pDate = photoDate(original)
yr = pDate.year
mo = pDate.month
if not os.path.exists(destDir + ‘/%04d/%02d’ % (yr, mo)):
os.makedirs(destDir + ‘/%04d/%02d’ % (yr, mo))
print ‘Creating directory ‘ + destDir + ‘/%04d/%02d’ % (yr, mo)
destinationfile = destDir + ‘/%04d/%02d/’ % (yr, mo) + photo
if os.path.exists(destinationfile):
#shutil.copy2(original, destinationfile)
print ‘File exists, skipping ‘ + photo
if not os.path.exists(destinationfile):
shutil.copy2(original, destinationfile)
print destinationfile
os.system(‘read -s -n 1 -p “Press any key to continue…”‘)
os.popen(“””osascript -e ‘tell application “Finder” to eject “NIKON”‘”””)
Download the script by clicking Import-photos.command