Created
May 4, 2012 10:22
-
-
Save KelSolaar/2593864 to your computer and use it in GitHub Desktop.
Calculate FStops Difference Between Two Images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
import math | |
import re | |
def unpackDefault(data, length=3, default=None): | |
""" | |
This definition unpacks given sequence enforcing a given length. | |
:param data: Sequence to unpack. ( Tuple / List ) | |
:param length: Default length to unpack to. ( Integer ) | |
:param default: Default fill object. ( Object) | |
:return: Unpacked sequence. ( Iterator ) | |
""" | |
return itertools.islice(itertools.chain(iter(data), itertools.repeat(default)), length) | |
def parseShutterSpeed(shutterSpeed): | |
""" | |
This definition parses given shutter speed data. | |
:param shutterSpeed: Shutter speed data. ( String ) | |
:return: Parser shutter data. ( Float ) | |
""" | |
shutterSpeed = tuple(float(re.match(r"[-+]?\d*\.?\d+([eE][-+]?\d+)?", token).group(0)) for token in unpackDefault(shutterSpeed.split("/"), 2, str(1))) | |
return shutterSpeed[1] != 0 and shutterSpeed[0] / shutterSpeed[1] or None | |
def getFStops(metadataA, metadataB): | |
""" | |
This definition returns the fStops difference between given data. | |
:param metadataA: Shutter speed / aperture data. ( Tuple / List ) | |
:param metadataB: Shutter speed / aperture data. ( Tuple / List ) | |
:return: Exposure ratio left / right, fStops. ( Tuple ) | |
""" | |
shutterSpeedA, apertureA, = (float(data) for data in metadataA) | |
shutterSpeedB, apertureB, = (float(data) for data in metadataB) | |
shutterSpeedRatio = shutterSpeedA / shutterSpeedB | |
apertureRatio = apertureB / apertureA | |
exposureRatio = shutterSpeedRatio * apertureRatio * apertureRatio | |
if exposureRatio > 0.999 and exposureRatio < 1.001: | |
exposureRatio=1 | |
if exposureRatio > 1: | |
left = round(exposureRatio * 1000) / 1000 | |
right = 1 | |
elif exposureRatio < 1: | |
left = 1 | |
exposureRatio = 1 / exposureRatio | |
right = round(exposureRatio * 1000) / 1000 | |
else: | |
left = right = 1 | |
fStops = math.log(exposureRatio) / math.log(2) | |
return left, right, fStops | |
#print parseShutterSpeed("1/125") | |
#print parseShutterSpeed("1.29684") | |
#print parseShutterSpeed("1/3.08442 Sec.") | |
print getFStops((parseShutterSpeed("1/125"), 4), | |
(parseShutterSpeed("1/250"), 2.8)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment