#!/usr/bin/python -tt """ dcraw(1) wrapper to generate jpeg thumbnails from raw pictures in parallel Written by Martin Pelikan, but actually in public domain. dcraw(1) was written by Dave Coffin, and is available at: http://www.cybercom.net/~dcoffin/dcraw/ """ THUMB_DIR = "_thumbnails" import errno, os, sys, time import multiprocessing, subprocess def generate_thumbnail(rawname): jpgname = rawname.rsplit(".", 1)[0] + ".jpg" p = jpgname.rsplit("/", 1) if len(p) > 1: jpgname = p[1] jpgname = os.path.join(THUMB_DIR, jpgname) start = time.time() dcraw_args = [ "dcraw", "-c", rawname ] convert_args = [ "convert", "-", "-resize", "50%", jpgname ] d = subprocess.Popen(dcraw_args, stdout=subprocess.PIPE) c = subprocess.Popen(convert_args, stdin=d.stdout) d.stdout.close() os.waitpid(d.pid, 0) os.waitpid(c.pid, 0) total = time.time() - start print("finished", rawname, "in", "{0:.2f} seconds".format(total)) return total if __name__ == '__main__': start = time.time() raws = [] dirs = [] dirs.append("." if len(sys.argv) == 1 else sys.argv[1]) while len(dirs) > 0: curdir = dirs.pop() for file in os.listdir(curdir): f = os.path.join(curdir, file) if os.path.isdir(f): dirs.append(f) elif not os.path.isfile(f): continue arr = file.rsplit(".", 1) if len(arr) > 1 and arr[1].lower() == 'cr2': raws.append(f) try: os.mkdir(THUMB_DIR) except OSError as e: if e.errno != errno.EEXIST: print(e) sys.exit(e.errno) # at the beginning, dcraw(1) does something cpu non-intensive # let the kernel scheduler take care of that units = multiprocessing.cpu_count() + 2 pool = multiprocessing.Pool(units) times = pool.map(generate_thumbnail, raws) total = time.time() - start sumtimes = sum(times) avg = sumtimes/len(times) speedup = sumtimes/total print("Average {0:.2f} sec, total {1:.2f} sec, relative speedup {2:.2f}x".format(avg, total, speedup))