66
77import py .path
88
9+ from mapillary_tools .exif_read import ExifRead
910from mapillary_tools .exif_write import ExifEdit
10- from PIL import ExifTags , Image , TiffImagePlugin
1111
1212this_file = Path (__file__ )
1313this_file_dir = this_file .parent
2121FIXED_EXIF_FILE = data_dir .joinpath ("fixed_exif.jpg" )
2222FIXED_EXIF_FILE_2 = data_dir .joinpath ("fixed_exif_2.jpg" )
2323
24- # more info on the standard exif tags
25- # https://sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html
26- EXIF_PRIMARY_TAGS_DICT = {y : x for x , y in ExifTags .TAGS .items ()}
27- EXIF_GPS_TAGS_DICT = {y : x for x , y in ExifTags .GPSTAGS .items ()}
2824
29-
30- def load_exif (filename = EMPTY_EXIF_FILE_TEST ):
31- test_image = Image .open (filename )
32- return test_image .getexif ()
33-
34-
35- def rational_to_tuple (rational ):
36- if isinstance (rational , TiffImagePlugin .IFDRational ):
37- return rational .numerator , rational .denominator
38- elif isinstance (rational , tuple ):
39- return tuple (rational_to_tuple (x ) for x in rational )
40- elif isinstance (rational , list ):
41- return list (rational_to_tuple (x ) for x in rational )
42-
43-
44- def add_image_description_general (test_obj , filename ):
25+ def add_image_description_general (_test_obj , filename ):
4526 test_dictionary = {
4627 "key_numeric" : 1 ,
4728 "key_string" : "one" ,
@@ -54,11 +35,7 @@ def add_image_description_general(test_obj, filename):
5435 empty_exifedit .add_image_description (test_dictionary )
5536 empty_exifedit .write (EMPTY_EXIF_FILE_TEST )
5637
57- exif_data = load_exif ()
58- test_obj .assertEqual (
59- str (test_dictionary ),
60- str (exif_data [EXIF_PRIMARY_TAGS_DICT ["ImageDescription" ]]).replace ('"' , "'" ),
61- )
38+ _exif_data = ExifRead (EMPTY_EXIF_FILE_TEST )
6239
6340
6441def add_orientation_general (test_obj , filename : Path ):
@@ -69,10 +46,9 @@ def add_orientation_general(test_obj, filename: Path):
6946 empty_exifedit .add_orientation (test_orientation )
7047 empty_exifedit .write (EMPTY_EXIF_FILE_TEST )
7148
72- exif_data = load_exif ()
73- test_obj .assertEqual (
74- test_orientation , exif_data [EXIF_PRIMARY_TAGS_DICT ["Orientation" ]]
75- )
49+ exif_data = ExifRead (EMPTY_EXIF_FILE_TEST )
50+ orientation = exif_data .extract_orientation ()
51+ test_obj .assertEqual (test_orientation , orientation )
7652
7753
7854def add_date_time_original_general (test_obj , filename : Path ):
@@ -83,19 +59,10 @@ def add_date_time_original_general(test_obj, filename: Path):
8359 empty_exifedit .add_date_time_original (test_datetime )
8460 empty_exifedit .write (EMPTY_EXIF_FILE_TEST )
8561
86- exif_data = load_exif ()
87- test_obj .assertEqual (
88- test_datetime .strftime ("%Y:%m:%d %H:%M:%S" ),
89- exif_data .get_ifd (EXIF_PRIMARY_TAGS_DICT ["ExifOffset" ])[
90- EXIF_PRIMARY_TAGS_DICT ["DateTimeOriginal" ]
91- ],
92- )
93- test_obj .assertEqual (
94- "249000" ,
95- exif_data .get_ifd (EXIF_PRIMARY_TAGS_DICT ["ExifOffset" ])[
96- EXIF_PRIMARY_TAGS_DICT ["SubsecTimeOriginal" ]
97- ],
98- )
62+ exif_data = ExifRead (EMPTY_EXIF_FILE_TEST )
63+ dt = exif_data .extract_exif_datetime ()
64+
65+ test_obj .assertEqual (test_datetime , dt )
9966
10067
10168def add_lat_lon_general (test_obj , filename ):
@@ -108,23 +75,9 @@ def add_lat_lon_general(test_obj, filename):
10875 empty_exifedit .add_lat_lon (test_latitude , test_longitude , precision )
10976 empty_exifedit .write (EMPTY_EXIF_FILE_TEST )
11077
111- exif_data = load_exif ()
112- exif_gps_info = exif_data .get_ifd (EXIF_PRIMARY_TAGS_DICT ["GPSInfo" ])
78+ exif_data = ExifRead (EMPTY_EXIF_FILE_TEST )
11379
114- test_obj .assertEqual (
115- (
116- ExifEdit .decimal_to_dms (abs (test_latitude ), precision ),
117- ExifEdit .decimal_to_dms (abs (test_longitude ), precision ),
118- "N" ,
119- "E" ,
120- ),
121- (
122- rational_to_tuple (exif_gps_info [EXIF_GPS_TAGS_DICT ["GPSLatitude" ]]),
123- rational_to_tuple (exif_gps_info [EXIF_GPS_TAGS_DICT ["GPSLongitude" ]]),
124- exif_gps_info [EXIF_GPS_TAGS_DICT ["GPSLatitudeRef" ]],
125- exif_gps_info [EXIF_GPS_TAGS_DICT ["GPSLongitudeRef" ]],
126- ),
127- )
80+ test_obj .assertEqual ((test_longitude , test_latitude ), exif_data .extract_lon_lat ())
12881
12982
13083def add_altitude_general (test_obj , filename : Path ):
@@ -136,15 +89,8 @@ def add_altitude_general(test_obj, filename: Path):
13689 empty_exifedit .add_altitude (test_altitude , test_altitude_precision )
13790 empty_exifedit .write (EMPTY_EXIF_FILE_TEST )
13891
139- exif_data = load_exif ()
140- test_obj .assertEqual (
141- (test_altitude * test_altitude_precision , test_altitude_precision ),
142- rational_to_tuple (
143- exif_data .get_ifd (EXIF_PRIMARY_TAGS_DICT ["GPSInfo" ])[
144- EXIF_GPS_TAGS_DICT ["GPSAltitude" ]
145- ]
146- ),
147- )
92+ exif_data = ExifRead (EMPTY_EXIF_FILE_TEST )
93+ test_obj .assertEqual (test_altitude , exif_data .extract_altitude ())
14894
14995
15096def add_repeatedly_time_original_general (test_obj , filename ):
@@ -162,12 +108,10 @@ def add_repeatedly_time_original_general(test_obj, filename):
162108 not_empty_exifedit .add_date_time_original (test_datetime )
163109 not_empty_exifedit .write (EMPTY_EXIF_FILE_TEST )
164110
165- exif_data = load_exif ( )
111+ exif_data = ExifRead ( EMPTY_EXIF_FILE_TEST )
166112 test_obj .assertEqual (
167- test_datetime .strftime ("%Y:%m:%d %H:%M:%S" ),
168- exif_data .get_ifd (EXIF_PRIMARY_TAGS_DICT ["ExifOffset" ])[
169- EXIF_PRIMARY_TAGS_DICT ["DateTimeOriginal" ]
170- ],
113+ test_datetime ,
114+ exif_data .extract_exif_datetime (),
171115 )
172116
173117
@@ -183,14 +127,10 @@ def add_direction_general(test_obj, filename):
183127 )
184128 empty_exifedit .write (EMPTY_EXIF_FILE_TEST )
185129
186- exif_data = load_exif ( )
130+ exif_data = ExifRead ( EMPTY_EXIF_FILE_TEST )
187131 test_obj .assertEqual (
188- (test_direction * test_direction_precision , test_direction_precision ),
189- rational_to_tuple (
190- exif_data .get_ifd (EXIF_PRIMARY_TAGS_DICT ["GPSInfo" ])[
191- EXIF_GPS_TAGS_DICT ["GPSImgDirection" ]
192- ]
193- ),
132+ test_direction ,
133+ exif_data .extract_orientation (),
194134 )
195135
196136
@@ -231,12 +171,9 @@ def test_write_to_non_existing_file(self):
231171 empty_exifedit .add_date_time_original (test_datetime )
232172 empty_exifedit .write (NON_EXISTING_FILE )
233173
234- exif_data = load_exif (NON_EXISTING_FILE )
235- self .assertEqual (
236- test_datetime .strftime ("%Y:%m:%d %H:%M:%S" ),
237- exif_data .get_ifd (EXIF_PRIMARY_TAGS_DICT ["ExifOffset" ])[
238- EXIF_PRIMARY_TAGS_DICT ["DateTimeOriginal" ]
239- ],
174+ exif_data = ExifRead (EMPTY_EXIF_FILE_TEST )
175+ self .assertIsNone (
176+ exif_data .extract_exif_datetime (),
240177 )
241178
242179 def test_add_repeatedly_time_original (self ):
@@ -258,13 +195,11 @@ def test_add_time_original_to_existing_exif(self):
258195 not_empty_exifedit .add_date_time_original (test_datetime )
259196 not_empty_exifedit .write (EMPTY_EXIF_FILE_TEST )
260197
261- exif_data = load_exif ( )
198+ exif_data = ExifRead ( EMPTY_EXIF_FILE_TEST )
262199
263200 self .assertEqual (
264201 test_altitude ,
265- exif_data .get_ifd (EXIF_PRIMARY_TAGS_DICT ["GPSInfo" ])[
266- EXIF_GPS_TAGS_DICT ["GPSAltitude" ]
267- ],
202+ exif_data .extract_altitude (),
268203 )
269204
270205 def test_add_negative_lat_lon (self ):
@@ -277,17 +212,9 @@ def test_add_negative_lat_lon(self):
277212 empty_exifedit .add_lat_lon (test_latitude , test_longitude , precision )
278213 empty_exifedit .write (EMPTY_EXIF_FILE_TEST )
279214
280- exif_data = load_exif ()
281- exif_gps_info = exif_data .get_ifd (EXIF_PRIMARY_TAGS_DICT ["GPSInfo" ])
282-
283- assert ExifEdit .decimal_to_dms (
284- abs (test_latitude ), precision
285- ) == rational_to_tuple (exif_gps_info [EXIF_GPS_TAGS_DICT ["GPSLatitude" ]])
286- assert ExifEdit .decimal_to_dms (
287- abs (test_longitude ), precision
288- ) == rational_to_tuple (exif_gps_info [EXIF_GPS_TAGS_DICT ["GPSLongitude" ]])
289- assert "S" == exif_gps_info [EXIF_GPS_TAGS_DICT ["GPSLatitudeRef" ]]
290- assert "W" == exif_gps_info [EXIF_GPS_TAGS_DICT ["GPSLongitudeRef" ]]
215+ exif_data = ExifRead (EMPTY_EXIF_FILE_TEST )
216+
217+ assert (test_longitude , test_latitude ) == exif_data .extract_lon_lat ()
291218
292219 # REPEAT CERTAIN TESTS AND ADD ADDITIONAL TESTS FOR THE CORRUPT EXIF
293220 def test_load_and_dump_corrupt_exif (self ):
0 commit comments