@@ -242,7 +242,12 @@ def test_additional_metadata(
242
242
243
243
im .save (out , tiffinfo = new_ifd )
244
244
245
- def test_custom_metadata (self , tmp_path : Path ) -> None :
245
+ @pytest .mark .parametrize ("libtiff" , (True , False ))
246
+ def test_custom_metadata (
247
+ self , monkeypatch : pytest .MonkeyPatch , tmp_path : Path , libtiff : bool
248
+ ) -> None :
249
+ monkeypatch .setattr (TiffImagePlugin , "WRITE_LIBTIFF" , libtiff )
250
+
246
251
class Tc (NamedTuple ):
247
252
value : Any
248
253
type : int
@@ -281,53 +286,43 @@ class Tc(NamedTuple):
281
286
)
282
287
}
283
288
284
- libtiffs = [False ]
285
- if Image .core .libtiff_support_custom_tags :
286
- libtiffs .append (True )
287
-
288
- for libtiff in libtiffs :
289
- TiffImagePlugin .WRITE_LIBTIFF = libtiff
290
-
291
- def check_tags (
292
- tiffinfo : TiffImagePlugin .ImageFileDirectory_v2 | dict [int , str ]
293
- ) -> None :
294
- im = hopper ()
295
-
296
- out = str (tmp_path / "temp.tif" )
297
- im .save (out , tiffinfo = tiffinfo )
298
-
299
- with Image .open (out ) as reloaded :
300
- for tag , value in tiffinfo .items ():
301
- reloaded_value = reloaded .tag_v2 [tag ]
302
- if (
303
- isinstance (reloaded_value , TiffImagePlugin .IFDRational )
304
- and libtiff
305
- ):
306
- # libtiff does not support real RATIONALS
307
- assert (
308
- round (abs (float (reloaded_value ) - float (value )), 7 ) == 0
309
- )
310
- continue
311
-
312
- assert reloaded_value == value
313
-
314
- # Test with types
315
- ifd = TiffImagePlugin .ImageFileDirectory_v2 ()
316
- for tag , tagdata in custom .items ():
317
- ifd [tag ] = tagdata .value
318
- ifd .tagtype [tag ] = tagdata .type
319
- check_tags (ifd )
320
-
321
- # Test without types. This only works for some types, int for example are
322
- # always encoded as LONG and not SIGNED_LONG.
323
- check_tags (
324
- {
325
- tag : tagdata .value
326
- for tag , tagdata in custom .items ()
327
- if tagdata .supported_by_default
328
- }
329
- )
330
- TiffImagePlugin .WRITE_LIBTIFF = False
289
+ def check_tags (
290
+ tiffinfo : TiffImagePlugin .ImageFileDirectory_v2 | dict [int , str ]
291
+ ) -> None :
292
+ im = hopper ()
293
+
294
+ out = str (tmp_path / "temp.tif" )
295
+ im .save (out , tiffinfo = tiffinfo )
296
+
297
+ with Image .open (out ) as reloaded :
298
+ for tag , value in tiffinfo .items ():
299
+ reloaded_value = reloaded .tag_v2 [tag ]
300
+ if (
301
+ isinstance (reloaded_value , TiffImagePlugin .IFDRational )
302
+ and libtiff
303
+ ):
304
+ # libtiff does not support real RATIONALS
305
+ assert round (abs (float (reloaded_value ) - float (value )), 7 ) == 0
306
+ continue
307
+
308
+ assert reloaded_value == value
309
+
310
+ # Test with types
311
+ ifd = TiffImagePlugin .ImageFileDirectory_v2 ()
312
+ for tag , tagdata in custom .items ():
313
+ ifd [tag ] = tagdata .value
314
+ ifd .tagtype [tag ] = tagdata .type
315
+ check_tags (ifd )
316
+
317
+ # Test without types. This only works for some types, int for example are
318
+ # always encoded as LONG and not SIGNED_LONG.
319
+ check_tags (
320
+ {
321
+ tag : tagdata .value
322
+ for tag , tagdata in custom .items ()
323
+ if tagdata .supported_by_default
324
+ }
325
+ )
331
326
332
327
def test_osubfiletype (self , tmp_path : Path ) -> None :
333
328
outfile = str (tmp_path / "temp.tif" )
@@ -682,8 +677,7 @@ def test_exif_ifd(self, tmp_path: Path) -> None:
682
677
im .save (outfile )
683
678
684
679
with Image .open (outfile ) as reloaded :
685
- if Image .core .libtiff_support_custom_tags :
686
- assert reloaded .tag_v2 [34665 ] == 125456
680
+ assert reloaded .tag_v2 [34665 ] == 125456
687
681
688
682
def test_crashing_metadata (
689
683
self , monkeypatch : pytest .MonkeyPatch , tmp_path : Path
@@ -735,19 +729,7 @@ def test_read_icc(self, monkeypatch: pytest.MonkeyPatch) -> None:
735
729
assert icc_libtiff is not None
736
730
assert icc == icc_libtiff
737
731
738
- @pytest .mark .parametrize (
739
- "libtiff" ,
740
- (
741
- pytest .param (
742
- True ,
743
- marks = pytest .mark .skipif (
744
- not Image .core .libtiff_support_custom_tags ,
745
- reason = "Custom tags not supported by older libtiff" ,
746
- ),
747
- ),
748
- False ,
749
- ),
750
- )
732
+ @pytest .mark .parametrize ("libtiff" , (True , False ))
751
733
def test_write_icc (
752
734
self , monkeypatch : pytest .MonkeyPatch , tmp_path : Path , libtiff : bool
753
735
) -> None :
0 commit comments