File size: 4,579 Bytes
b2e2343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
Contains functionality for analyzing the exif data of an image.
"""

from __future__ import division

import math
import exifread


def analyze_background_blur(tags):
    """Calculates and returns an estimated hyperfocal distance that tells
    how far an object must be to be in focus. Values near zero mean
    hyperfocal distance is so low, that most likely everything in the
    picture is sharp. Values near 1 mean that all the objects has to be
    hundreds of meters away to be in focus.

    :param tags: the exif tags
    :type tags: dict
    :returns: float -- the estimated hyperfocal distance, None if not able
                       to calculate the value due to missing EXIF value
    """
    if tags and "EXIF FocalLength" in tags:
        if "EXIF FNumber" in tags:
            aperture = eval(str(tags["EXIF FNumber"]))
        elif "EXIF ApertureValue" in tags:
            aperture = eval(str(tags["EXIF ApertureValue"]))
        else:
            return None

        focal = eval(str(tags["EXIF FocalLength"]))
        return get_background_blur_ratio(focal, aperture)

    return None


def analyze_picture_exposure(tags):
    """Parses exif from given image and returns a float between 0 and 1,
    where values closer to 0 indicate low motion blur probability and
    values closer to 1 indicate high motion-blur probability.

    :param tags: the exif tags
    :type tags: dict
    :returns: float -- the estimated motion blur probability, None if not
                       able to calculate the value due to missing EXIF value
    """
    if tags and "EXIF ExposureTime" in tags:
        exposure = tags["EXIF ExposureTime"]
        exposure = eval(str(exposure))
        return get_exposure_ratio(exposure)
    return None


def get_exposure_value(tags):
    """Extracts exposure value from given tags
    """
    if tags and "EXIF ExposureTime" in tags:
        exposure = tags["EXIF ExposureTime"]
        exposure = eval(str(exposure))
        return exposure
    return None


def get_focal_value(tags):
    """Extracts focal length value from given tags
    """
    if tags and "EXIF FocalLength" in tags:
        return eval(str(tags["EXIF FocalLength"]))
    return None


def get_iso_value(tags):
    """Extracts ISO value from given tags
    """
    if tags and "EXIF ISOSpeedRatings" in tags:
        iso = tags["EXIF ISOSpeedRatings"]
        iso = eval(str(iso))
        return iso
    return None


def get_aperture_value(tags):
    """Extracts aperture values from given tags if focal
    length also exists
    """
    if tags and "EXIF FocalLength" in tags:
        if "EXIF FNumber" in tags:
            return eval(str(tags["EXIF FNumber"]))
        elif "EXIF ApertureValue" in tags:
            return eval(str(tags["EXIF ApertureValue"]))
        else:
            return None
    return None


def get_background_blur_ratio(focal, aperture):
    """Calculates hyperfocal length from given focal and aperture values
    and returns an estimation on how likely the background is blurred
    """
    if aperture < 0.001:
        return None

    # circle of confusion size
    coc = 0.015

    # hyperfocal calculation
    hyperfocal = focal ** 2 / (aperture * coc) + focal

    # hyperfocal thresholds in millimeters
    min_threshold = 200
    max_threshold = 100000

    # normalize:
    hyperfocal = math.log(hyperfocal, 2)
    min_threshold = math.log(min_threshold, 2)
    max_threshold = math.log(max_threshold, 2)

    hyperfocal = (hyperfocal - min_threshold) / (max_threshold - min_threshold)

    if hyperfocal < 0:
        return 0.0
    if hyperfocal > 1:
        return 1.0

    return hyperfocal


def get_exposure_ratio(exposure):
    """Calculates the probability of motion blur in an image using
    the given exposure time. Exposure of over 1/4 seconds is most likely
    to be motion blurred.
    """
    # min and max exposure threshold values
    min_exposure = 1 / 2000
    max_exposure = 1 / 4

    # normalize
    exposure = math.log(exposure, 2)
    min_exposure = math.log(min_exposure, 2)
    max_exposure = math.log(max_exposure, 2)

    exposure = (exposure - min_exposure) / (max_exposure - min_exposure)

    if exposure < 0:
        return 0.0
    if exposure > 1:
        return 1.0

    return exposure


def parse_exif(image_path):
    """Parses the exif tags from an image

    :param image_path: path to the image file
    :type image_path: str
    :returns: dict -- the exif tags
    """
    with open(image_path, 'rb') as image:
        return exifread.process_file(image, details=False)