1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 from mediagoblin
.tools
.exif
import exif_fix_image_orientation
, \
22 extract_exif
, clean_exif
, get_gps_data
, get_useful
24 GOOD_JPG
= pkg_resources
.resource_filename(
29 EMPTY_JPG
= pkg_resources
.resource_filename(
34 BAD_JPG
= pkg_resources
.resource_filename(
39 GPS_JPG
= pkg_resources
.resource_filename(
45 def test_exif_extraction():
47 Test EXIF extraction from a good image
49 result
= extract_exif(GOOD_JPG
)
50 clean
= clean_exif(result
)
51 useful
= get_useful(clean
)
52 gps
= get_gps_data(result
)
54 # Do we have the result?
55 assert len(result
) == 108
57 # Do we have clean data?
58 assert len(clean
) == 105
63 # Do we have the "useful" tags?
72 'EXIF ExposureTime': {
88 'printable': 'NIKON D80',
91 'values': 'NIKON D80',
95 'printable': 'NIKON CORPORATION',
98 'values': 'NIKON CORPORATION',
100 'EXIF ExposureMode': {
102 'printable': 'Manual Exposure',
107 'EXIF ISOSpeedRatings': {
121 'EXIF UserComment': {
123 'printable': 'Joar Wandborg ',
124 'field_offset': 26180,
127 65, 83, 67, 73, 73, 0, 0, 0, 74, 111, 97, 114, 32, 87,
128 97, 110, 100, 98, 111, 114, 103, 32, 32, 32, 32, 32, 32,
129 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
133 def test_exif_image_orientation():
135 Test image reorientation based on EXIF data
137 result
= extract_exif(GOOD_JPG
)
139 image
= exif_fix_image_orientation(
140 Image
.open(GOOD_JPG
),
143 # Are the dimensions correct?
144 assert image
.size
== (428, 640)
146 # If this pixel looks right, the rest of the image probably will too.
147 assert image
.getdata()[10000] == (41, 28, 11)
149 def test_exif_no_exif():
151 Test an image without exif
153 result
= extract_exif(EMPTY_JPG
)
154 clean
= clean_exif(result
)
155 useful
= get_useful(clean
)
156 gps
= get_gps_data(result
)
163 def test_exif_bad_image():
165 Test EXIF extraction from a faithful, but bad image
167 result
= extract_exif(BAD_JPG
)
168 clean
= clean_exif(result
)
169 useful
= get_useful(clean
)
170 gps
= get_gps_data(result
)
177 def test_exif_gps_data():
179 Test extractiion of GPS data
181 result
= extract_exif(GPS_JPG
)
182 gps
= get_gps_data(result
)
185 'latitude': 59.336666666666666,
186 'direction': 25.674046740467404,
187 'altitude': 37.64365671641791,
188 'longitude': 18.016166666666667}