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
) == 56
63 # Do we have clean data?
64 assert len(clean
) == 53
69 # Do we have the "useful" tags?
73 'printable': u
'Flash did not fire',
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': {
129 def test_exif_image_orientation():
131 Test image reorientation based on EXIF data
133 result
= extract_exif(GOOD_JPG
)
135 image
= exif_fix_image_orientation(
136 Image
.open(GOOD_JPG
),
139 # Are the dimensions correct?
140 assert image
.size
== (428, 640)
142 # If this pixel looks right, the rest of the image probably will too.
143 assert_in(image
.getdata()[10000],
144 ((41, 28, 11), (43, 27, 11))
148 def test_exif_no_exif():
150 Test an image without exif
152 result
= extract_exif(EMPTY_JPG
)
153 clean
= clean_exif(result
)
154 useful
= get_useful(clean
)
155 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
)
178 def test_exif_gps_data():
180 Test extractiion of GPS data
182 result
= extract_exif(GPS_JPG
)
183 gps
= get_gps_data(result
)
186 'latitude': 59.336666666666666,
187 'direction': 25.674046740467404,
188 'altitude': 37.64365671641791,
189 'longitude': 18.016166666666667}