Skip to content

Instantly share code, notes, and snippets.

@JustinAzoff
Forked from jlintz/focal_length.py
Created December 28, 2010 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinAzoff/757269 to your computer and use it in GitHub Desktop.
Save JustinAzoff/757269 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Author: Justin Lintz
# usage: ./focal_length dir
# Requiresments: dcraw http://www.cybercom.net/~dcoffin/dcraw/
import os
import sys
from subprocess import Popen, PIPE
from operator import itemgetter
DCRAW='dcraw' # or /path/to/dcraw
def count(it):
d={}
for x in it:
d[x]=d.get(x,0)+1
l = d.items()
l.sort(key=itemgetter(1),reverse=True)
return l
def get_focal_length(filename):
p = Popen([DCRAW,'-i','-v',filename],stdout=PIPE)
for line in p.stdout:
if line.startswith('Focal'):
length = line.split(':')[1].strip()
focal = length[:-2]
return focal
def get_raw_files(path):
file_list = []
for root,dirs,files in os.walk(path):
for filename in files:
if filename.endswith('CR2'):
file_list.append(os.path.join(root,filename))
return file_list
def main(path):
files = get_raw_files(path)
print 'Total pics to process: %d' % len(files)
focal_lengths = (get_focal_length(filename) for filename in files)
focal_counts = count(focal_lengths)
for length,num in focal_counts:
print 'Length: %s , Count: %d' % (length,num)
return 0
if __name__ == "__main__":
path = sys.argv[1]
main(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment