photo_transfer.py

import os
import re
import shutil
import stat
import string
import sys
import win32api

# adjust these to suit your installation and preferences
image_dir = 'D:\\Jim\\My Documents\\My Pictures\\Nikon 4500'
raw2nef = 'C:\\Extras\\raw2nef_v0.13.2\\raw2nefw'
dcraw = 'C:\\Extras\\dcraw-win32-1.224-P4\\dcraw'
netpbm = 'C:\\Extras\\GnuWin32\\bin'
# this size is for Coolpix 4500, adjust to suit your camera
raw_size = 5865472

# Attempt to identify camera's "drive" letter
def camera_drive():
    drives = string.splitfields(win32api.GetLogicalDriveStrings(),'\000')
    drives.remove('')
    for d in drives:
        try:
            (name, serial_number, max_comp_len, flags, system) = \
                   win32api.GetVolumeInformation(d)
            if name == '' and serial_number == 0 and \
               flags == 6 and system == 'FAT':
                return d
        except Exception, e:
            pass
    return None

def make_dir(path):
    if os.access(path, os.F_OK):
        return
    make_dir(os.path.dirname(path))
    os.mkdir(path)

# Program starts here...
drive = camera_drive()
if not drive:
    print "Could not connect to camera"
    sys.exit(1)

print "Camera is drive %s" % drive

# Get list of pictures to copy
src_files = []
for root, dirs, files in os.walk(drive):
    for name in files:
        ext = os.path.splitext(name)[1]
        if ext == '.JPG' or ext == '.TIF':
            src_files.append(os.path.join(root, name))
src_files.sort()

# Make destination directory
dest_dir = os.path.join(image_dir, "%s-%s" % (
    os.path.basename(src_files[0])[0:8],
    os.path.basename(src_files[-1])[4:8]))
make_dir(dest_dir)

# Change working directory
os.chdir(dest_dir)

# Copy files
for src_path in src_files:
    print "Copying %s" % (src_path)
    shutil.copy2(src_path, dest_dir)

# Convert raw images
dest_files = os.listdir('')
dest_files.sort()
for file in dest_files:
    (base, ext) = os.path.splitext(file)
    if ext != '.JPG':
        continue
    size = os.stat(file)[stat.ST_SIZE]
    if size != raw_size:
        continue
    print "Converting %s" % (file)
    # raw to nef
    if os.system("%s %s" % (raw2nef, file)) != 0:
        print "raw2nef failed"
        sys.exit(1)
    os.unlink(file)
    # nef to photoshop
#    if os.system("%s -3 %s.NEF" % (dcraw, base)) != 0:
#        print "nef to photoshop failed"
#        sys.exit(1)
    # nef to fits
    if os.system("%s -c -g 1.0 -4 %s.NEF | %s\\pnmtofits >%s.FIT" %
                 (dcraw, base, netpbm, base)) != 0:
        print "nef to fits failed"
        sys.exit(1)