From 3ac0420d6cfdfc72c784ee9ee24a1c2dfb3b7757 Mon Sep 17 00:00:00 2001 From: Enric Pou Date: Wed, 21 May 2025 13:48:46 +0200 Subject: [PATCH] Fix: Set tag type accordingly if IFDRational with 0 denominator --- Tests/test_file_tiff_metadata.py | 25 +++++++++++++++++++++++++ Tests/test_tiff_ifdrational.py | 11 +++++++++++ src/PIL/TiffImagePlugin.py | 5 ++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index 884868345..fbaec7835 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -1,6 +1,7 @@ from __future__ import annotations import io +import math import struct from pathlib import Path @@ -280,6 +281,30 @@ def test_writing_other_types_to_undefined( assert reloaded.tag_v2[33723] == b"1" +@pytest.mark.parametrize( + "value, expected", + ( + (IFDRational(1, 0), TiffTags.RATIONAL), + (IFDRational(-1, 0), TiffTags.SIGNED_RATIONAL), + ), +) +def test_tagtype_on_zero_denominator( + value: IFDRational, expected: int, tmp_path: Path +) -> None: + info = TiffImagePlugin.ImageFileDirectory_v2() + + info[37380] = value + assert info.tagtype[37380] == expected + + im = hopper() + out = tmp_path / "temp.tiff" + im.save(out, tiffinfo=info) + + with Image.open(out) as reloaded: + assert isinstance(reloaded, TiffImagePlugin.TiffImageFile) + assert math.isnan(reloaded.tag_v2[37380]) + + def test_undefined_zero(tmp_path: Path) -> None: # Check that the tag has not been changed since this test was created tag = TiffTags.TAGS_V2[45059] diff --git a/Tests/test_tiff_ifdrational.py b/Tests/test_tiff_ifdrational.py index 42d06b896..5233cb2b9 100644 --- a/Tests/test_tiff_ifdrational.py +++ b/Tests/test_tiff_ifdrational.py @@ -74,3 +74,14 @@ def test_ifd_rational_save( with Image.open(out) as reloaded: assert isinstance(reloaded, TiffImagePlugin.TiffImageFile) assert float(IFDRational(301, 1)) == float(reloaded.tag_v2[282]) + + +@pytest.mark.parametrize( + "numerator, denominator, expected_result", + [ + (1, 1, 1.0), + (1, 0, float("nan")), + ], +) +def test_float_cast(numerator, denominator, expected_result): + float(IFDRational(numerator, denominator)) == expected_result diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 88af9162e..0147f79f2 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -402,6 +402,9 @@ class IFDRational(Rational): f = self._val.limit_denominator(max_denominator) return f.numerator, f.denominator + def __float__(self): + return float(self._val) + def __repr__(self) -> str: return str(float(self._val)) @@ -688,7 +691,7 @@ class ImageFileDirectory_v2(_IFDv2Base): if all(isinstance(v, IFDRational) for v in values): for v in values: assert isinstance(v, IFDRational) - if v < 0: + if v < 0 or (math.isnan(v) and v.numerator < 0): self.tagtype[tag] = TiffTags.SIGNED_RATIONAL break else: