#!/usr/bin/env python # Emil Erlandsson, emil.erlandsson@gmail.com import sys, os, time, shutil # My own module for cleaning up file names import namefix TRANSFER_CFG = { ".jpg": '/home/emil/Pictures', # JPEGs ".cr2" : '/home/emil/RawPhotos', # Canon RAW Version 2 ".crw": '/home/emil/RawPhotos' } # Canon RAW def load_images(path, config): file_list = [] for root, dirs, files in os.walk(path): for file in files: ext = os.path.splitext(file)[1].lower() if ext in config: #print "Loading %s..." % os.path.join(root, file) file_list.append(os.path.join(root, file)) return file_list def transfer_images(list, config, name): for image in list: ext = os.path.splitext(image)[1].lower() stat_info = os.stat(image) timeinfo = time.localtime(stat_info[os.path.stat.ST_CTIME]) year = "%04d" % timeinfo[0] month = "%02d" % timeinfo [1] day = "%02d" % timeinfo[2] out_dir = config[ext] if not out_dir is None: # Set up the out path like: EXT_OUTPATH/YYYY/YYYYMMDD-Description out_dir = os.path.join(out_dir, year) if not os.path.exists(out_dir): os.mkdir(out_dir) out_dir = os.path.join(out_dir, "%s%s%s-%s" % (year, month, day, name)) if not os.path.exists(out_dir): os.mkdir(out_dir) if os.path.exists(out_dir): out_file = os.path.join(out_dir, os.path.basename(image)) #print "Copying %s => %s" % (image, out_file) shutil.copy(image, out_file) else: print "Could not find output directory for %s" % ext def usage(): print """imagetransfer, v0.0.1 - Copyright (C) 2006 Emil Erlandsson Usage: imagetransfer.py [path] ([description] -d) \tWhere ... \t* [path] is the location where the images are stored. \t* [description] is optional. If not given, it will \n\t be asked for via stdin. \t* -d is also optional. If given (last) it will remove the\n\t original files. Use with care!""" if __name__ == "__main__": if not len(sys.argv) > 1: usage() sys.exit(1) else: if not os.path.exists(sys.argv[1]) and os.path.isdir(sys.argv[1]): usage() sys.exit(1) else: if not len(TRANSFER_CFG) > 0: print "No configuration. Exiting." sys.exit(1) image_path = sys.argv[1] image_list = load_images(image_path, TRANSFER_CFG) if len(image_list) > 0: print "%d images found in %s. Starting to transfer ..." % ( len(image_list), image_path) name = "untitled" if len(sys.argv) > 2: name = sys.argv[2] else: print "Enter a name for these photos: ", name = sys.stdin.readline() name = namefix.clean(name) transfer_images(image_list, TRANSFER_CFG, name) if len(sys.argv) > 3: if sys.argv[3] is "-d": for file in image_list: os.unlink(file) else: print "No images matching the filter found in %s" % image_path