aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tests/test_exif.py
blob: 5eeaa6763cac6236f2ac415992f8596d1c7285c9 (plain)
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
162
163
164
165
166
167
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import Image

from mediagoblin.tools.exif import exif_fix_image_orientation, \
    extract_exif, clean_exif, get_gps_data, get_useful
from .resources import GOOD_JPG, EMPTY_JPG, BAD_JPG, GPS_JPG


def assert_in(a, b):
    assert a in b, "%r not in %r" % (a, b)


def test_exif_extraction():
    '''
    Test EXIF extraction from a good image
    '''
    result = extract_exif(GOOD_JPG)
    clean = clean_exif(result)
    useful = get_useful(clean)
    gps = get_gps_data(result)

    # Do we have the result?
    assert len(result) == 56

    # Do we have clean data?
    assert len(clean) == 53

    # GPS data?
    assert gps == {}

    # Do we have the "useful" tags?
    assert useful == {
        'EXIF Flash': {
            'field_type': 3,
            'printable': u'Flash did not fire',
            'field_offset': 380,
            'tag': 37385,
            'values': [0],
            'field_length': 2},
        'EXIF ExposureTime': {
            'field_type': 5,
            'printable': '1/125',
            'field_offset': 700,
            'tag': 33434,
            'values': [[1, 125]],
            'field_length': 8},
        'EXIF FocalLength': {
            'field_type': 5,
            'printable': '18',
            'field_offset': 780,
            'tag': 37386,
            'values': [[18, 1]],
            'field_length': 8},
        'Image Model': {
            'field_type': 2,
            'printable': 'NIKON D80',
            'field_offset': 152,
            'tag': 272,
            'values': 'NIKON D80',
            'field_length': 10},
        'Image Make': {
            'field_type': 2,
            'printable': 'NIKON CORPORATION',
            'field_offset': 134,
            'tag': 271,
            'values': 'NIKON CORPORATION',
            'field_length': 18},
        'EXIF ExposureMode': {
            'field_type': 3,
            'printable': 'Manual Exposure',
            'field_offset': 584,
            'tag': 41986,
            'values': [1],
            'field_length': 2},
        'EXIF ISOSpeedRatings': {
            'field_type': 3,
            'printable': '100',
            'field_offset': 260,
            'tag': 34855,
            'values': [100],
            'field_length': 2},
        'EXIF FNumber': {
            'field_type': 5,
            'printable': '10',
            'field_offset': 708,
            'tag': 33437,
            'values': [[10, 1]],
            'field_length': 8}}


def test_exif_image_orientation():
    '''
    Test image reorientation based on EXIF data
    '''
    result = extract_exif(GOOD_JPG)

    image = exif_fix_image_orientation(
        Image.open(GOOD_JPG),
        result)

    # Are the dimensions correct?
    assert image.size == (428, 640)

    # If this pixel looks right, the rest of the image probably will too.
    assert_in(image.getdata()[10000],
              ((41, 28, 11), (43, 27, 11))
              )


def test_exif_no_exif():
    '''
    Test an image without exif
    '''
    result = extract_exif(EMPTY_JPG)
    clean = clean_exif(result)
    useful = get_useful(clean)
    gps = get_gps_data(result)

    assert result == {}
    assert clean == {}
    assert gps == {}
    assert useful == {}


def test_exif_bad_image():
    '''
    Test EXIF extraction from a faithful, but bad image
    '''
    result = extract_exif(BAD_JPG)
    clean = clean_exif(result)
    useful = get_useful(clean)
    gps = get_gps_data(result)

    assert result == {}
    assert clean == {}
    assert gps == {}
    assert useful == {}


def test_exif_gps_data():
    '''
    Test extractiion of GPS data
    '''
    result = extract_exif(GPS_JPG)
    gps = get_gps_data(result)

    assert gps == {
        'latitude': 59.336666666666666,
        'direction': 25.674046740467404,
        'altitude': 37.64365671641791,
        'longitude': 18.016166666666667}