1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 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
26 assert a
in b
, "%r not in %r" % (a
, b
)
29 GOOD_JPG
= pkg_resources
.resource_filename(
34 EMPTY_JPG
= pkg_resources
.resource_filename(
39 BAD_JPG
= pkg_resources
.resource_filename(
44 GPS_JPG
= pkg_resources
.resource_filename(
51 def test_exif_extraction():
53 Test EXIF extraction from a good image
55 result
= extract_exif(GOOD_JPG
)
56 clean
= clean_exif(result
)
57 useful
= get_useful(clean
)
58 gps
= get_gps_data(result
)
60 # Do we have the result?
61 assert len(result
) == 108
63 # Do we have clean data?
64 assert len(clean
) == 105
69 # Do we have the "useful" tags?
78 'EXIF ExposureTime': {
94 'printable': 'NIKON D80',
97 'values': 'NIKON D80',
101 'printable': 'NIKON CORPORATION',
104 'values': 'NIKON CORPORATION',
106 'EXIF ExposureMode': {
108 'printable': 'Manual Exposure',
113 'EXIF ISOSpeedRatings': {
127 'EXIF UserComment': {
129 'printable': 'Joar Wandborg ',
130 'field_offset': 26180,
133 65, 83, 67, 73, 73, 0, 0, 0, 74, 111, 97, 114, 32, 87,
134 97, 110, 100, 98, 111, 114, 103, 32, 32, 32, 32, 32, 32,
135 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
140 def test_exif_image_orientation():
142 Test image reorientation based on EXIF data
144 result
= extract_exif(GOOD_JPG
)
146 image
= exif_fix_image_orientation(
147 Image
.open(GOOD_JPG
),
150 # Are the dimensions correct?
151 assert image
.size
== (428, 640)
153 # If this pixel looks right, the rest of the image probably will too.
154 assert_in(image
.getdata()[10000],
155 ((41, 28, 11), (43, 27, 11))
159 def test_exif_no_exif():
161 Test an image without exif
163 result
= extract_exif(EMPTY_JPG
)
164 clean
= clean_exif(result
)
165 useful
= get_useful(clean
)
166 gps
= get_gps_data(result
)
174 def test_exif_bad_image():
176 Test EXIF extraction from a faithful, but bad image
178 result
= extract_exif(BAD_JPG
)
179 clean
= clean_exif(result
)
180 useful
= get_useful(clean
)
181 gps
= get_gps_data(result
)
189 def test_exif_gps_data():
191 Test extractiion of GPS data
193 result
= extract_exif(GPS_JPG
)
194 gps
= get_gps_data(result
)
197 'latitude': 59.336666666666666,
198 'direction': 25.674046740467404,
199 'altitude': 37.64365671641791,
200 'longitude': 18.016166666666667}