Skip to content

Instantly share code, notes, and snippets.

@codemartial
Last active May 30, 2018 00:47
Show Gist options
  • Save codemartial/495576af310c21f010d7 to your computer and use it in GitHub Desktop.
Save codemartial/495576af310c21f010d7 to your computer and use it in GitHub Desktop.
A Script to Analyse Effective Focal Length in Photographs
#!/usr/bin/python
import sys
full_imgsizes = {'NIKON D750': 6000,
'NIKON D7200': 6000,
'NIKON D3300': 6000,
'NIKON D7100': 6000,
'DSC-RX100': 5472,
'NEX-5': 4592,
'NIKON D90': 4288,
'NIKON D80': 3872}
aspect_ratios = {'NIKON D750': 1.5,
'NIKON D7200': 1.5,
'NIKON D7100': 1.5,
'NIKON D3300': 1.5,
'DSC-RX100': 1.5,
'NEX-5': 1.5,
'NIKON D90': 1.5,
'NIKON D80': 1.5}
limits = [16.0, 18.0, 20.0, 24.0, 28.0, 35.0, 50.0, 70.0, 85.0, 105.0, 135.0, 180.0, 200.0, 300.0, 400.0, 450.0, 500.0, 600.0, 800.0, 900.0, 1200.0, float("inf")]
def normalise(fls):
fls.sort()
l_idx = 0
fl_idx = 0
nfls = {}
for nfl in limits:
nfls[nfl] = 0
while l_idx < len(limits) and fl_idx < len(fls):
fl = fls[fl_idx]
if abs(limits[l_idx] - fl) < abs(limits[l_idx + 1] - fl):
nfls[limits[l_idx]] += 1
fl_idx += 1
else:
l_idx += 1
if l_idx == len(limits):
break
return nfls
def calculate_fl(model, imgsize, efocal):
if model not in full_imgsizes or imgsize == '' or efocal == '':
return 0.0
eq_fl = float(efocal.split(' ')[0])
x,y = imgsize.split('x')
long_edge, short_edge = max(float(x), float(y)), min(float(x), float(y))
if long_edge <= 0 or short_edge <= 0:
return 0.0
img_aspect = long_edge/short_edge
cam_aspect = aspect_ratios[model]
if img_aspect < cam_aspect:
long_edge = short_edge * cam_aspect
full_long_edge = full_imgsizes[model]
return eq_fl * full_long_edge/long_edge
def main():
fls = []
for line in sys.__stdin__.readlines():
(fname,model,imgsize,efocal) = line.strip().split(',')
fl = calculate_fl(model, imgsize, efocal)
if fl <= 0:
#print >> sys.stderr, line
sys.stderr.write(line)
continue
fls.append(fl)
nfls = normalise(fls)
for fl in limits:
print ("%.0f,%d" % (fl, nfls[fl]))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment