Skip to content

Commit

Permalink
Merge pull request #8000 from radarhere/libtiff
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Apr 22, 2024
2 parents 02915c7 + 0df8796 commit 1c21cd4
Showing 1 changed file with 56 additions and 49 deletions.
105 changes: 56 additions & 49 deletions Tests/test_file_libtiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,24 @@ def test_additional_metadata(

im.save(out, tiffinfo=new_ifd)

def test_custom_metadata(self, tmp_path: Path) -> None:
@pytest.mark.parametrize(
"libtiff",
(
pytest.param(
True,
marks=pytest.mark.skipif(
not getattr(Image.core, "libtiff_support_custom_tags", False),
reason="Custom tags not supported by older libtiff",
),
),
False,
),
)
def test_custom_metadata(
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path, libtiff: bool
) -> None:
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", libtiff)

class Tc(NamedTuple):
value: Any
type: int
Expand Down Expand Up @@ -281,53 +298,43 @@ class Tc(NamedTuple):
)
}

libtiffs = [False]
if Image.core.libtiff_support_custom_tags:
libtiffs.append(True)

for libtiff in libtiffs:
TiffImagePlugin.WRITE_LIBTIFF = libtiff

def check_tags(
tiffinfo: TiffImagePlugin.ImageFileDirectory_v2 | dict[int, str]
) -> None:
im = hopper()

out = str(tmp_path / "temp.tif")
im.save(out, tiffinfo=tiffinfo)

with Image.open(out) as reloaded:
for tag, value in tiffinfo.items():
reloaded_value = reloaded.tag_v2[tag]
if (
isinstance(reloaded_value, TiffImagePlugin.IFDRational)
and libtiff
):
# libtiff does not support real RATIONALS
assert (
round(abs(float(reloaded_value) - float(value)), 7) == 0
)
continue

assert reloaded_value == value

# Test with types
ifd = TiffImagePlugin.ImageFileDirectory_v2()
for tag, tagdata in custom.items():
ifd[tag] = tagdata.value
ifd.tagtype[tag] = tagdata.type
check_tags(ifd)

# Test without types. This only works for some types, int for example are
# always encoded as LONG and not SIGNED_LONG.
check_tags(
{
tag: tagdata.value
for tag, tagdata in custom.items()
if tagdata.supported_by_default
}
)
TiffImagePlugin.WRITE_LIBTIFF = False
def check_tags(
tiffinfo: TiffImagePlugin.ImageFileDirectory_v2 | dict[int, str]
) -> None:
im = hopper()

out = str(tmp_path / "temp.tif")
im.save(out, tiffinfo=tiffinfo)

with Image.open(out) as reloaded:
for tag, value in tiffinfo.items():
reloaded_value = reloaded.tag_v2[tag]
if (
isinstance(reloaded_value, TiffImagePlugin.IFDRational)
and libtiff
):
# libtiff does not support real RATIONALS
assert round(abs(float(reloaded_value) - float(value)), 7) == 0
continue

assert reloaded_value == value

# Test with types
ifd = TiffImagePlugin.ImageFileDirectory_v2()
for tag, tagdata in custom.items():
ifd[tag] = tagdata.value
ifd.tagtype[tag] = tagdata.type
check_tags(ifd)

# Test without types. This only works for some types, int for example are
# always encoded as LONG and not SIGNED_LONG.
check_tags(
{
tag: tagdata.value
for tag, tagdata in custom.items()
if tagdata.supported_by_default
}
)

def test_osubfiletype(self, tmp_path: Path) -> None:
outfile = str(tmp_path / "temp.tif")
Expand Down Expand Up @@ -741,7 +748,7 @@ def test_read_icc(self, monkeypatch: pytest.MonkeyPatch) -> None:
pytest.param(
True,
marks=pytest.mark.skipif(
not Image.core.libtiff_support_custom_tags,
not getattr(Image.core, "libtiff_support_custom_tags", False),
reason="Custom tags not supported by older libtiff",
),
),
Expand Down

0 comments on commit 1c21cd4

Please sign in to comment.