From 3ac0420d6cfdfc72c784ee9ee24a1c2dfb3b7757 Mon Sep 17 00:00:00 2001 From: Enric Pou Date: Wed, 21 May 2025 13:48:46 +0200 Subject: [PATCH 1/6] 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: From c1f0c23b47970c28ed286c104dad51d40db0a9bf Mon Sep 17 00:00:00 2001 From: Enric Pou Date: Thu, 22 May 2025 08:57:41 +0200 Subject: [PATCH 2/6] Mypy fix: Cast v.numerator to float --- src/PIL/TiffImagePlugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 0147f79f2..9bdf7c015 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -691,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 or (math.isnan(v) and v.numerator < 0): + if v < 0 or (math.isnan(v) and float(v.numerator) < 0): self.tagtype[tag] = TiffTags.SIGNED_RATIONAL break else: From b15c019ffca2cb2127694c709c234aa7a8566aee Mon Sep 17 00:00:00 2001 From: Enric Pou Date: Mon, 26 May 2025 09:21:12 +0200 Subject: [PATCH 3/6] Fix tests --- Tests/test_tiff_ifdrational.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Tests/test_tiff_ifdrational.py b/Tests/test_tiff_ifdrational.py index 5233cb2b9..15ef17f84 100644 --- a/Tests/test_tiff_ifdrational.py +++ b/Tests/test_tiff_ifdrational.py @@ -1,6 +1,7 @@ from __future__ import annotations from fractions import Fraction +import math from pathlib import Path import pytest @@ -84,4 +85,8 @@ def test_ifd_rational_save( ], ) def test_float_cast(numerator, denominator, expected_result): - float(IFDRational(numerator, denominator)) == expected_result + value = float(IFDRational(numerator, denominator)) + if math.isnan(expected_result): + assert value + else: + assert value == expected_result From 4fea45a02b44086b0f4cfe2c53ecee3cebde63b0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 26 May 2025 07:21:54 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- Tests/test_tiff_ifdrational.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_tiff_ifdrational.py b/Tests/test_tiff_ifdrational.py index 15ef17f84..1e42ff70e 100644 --- a/Tests/test_tiff_ifdrational.py +++ b/Tests/test_tiff_ifdrational.py @@ -1,7 +1,7 @@ from __future__ import annotations -from fractions import Fraction import math +from fractions import Fraction from pathlib import Path import pytest From 7eec948fa4f461780d20db1437105382512d6e74 Mon Sep 17 00:00:00 2001 From: Enric Pou Date: Wed, 28 May 2025 08:26:54 +0200 Subject: [PATCH 5/6] Add __float__ typing Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- src/PIL/TiffImagePlugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 9bdf7c015..0d6f3fba2 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -402,7 +402,7 @@ class IFDRational(Rational): f = self._val.limit_denominator(max_denominator) return f.numerator, f.denominator - def __float__(self): + def __float__(self) -> float: return float(self._val) def __repr__(self) -> str: From f2a2b1a13884750e9f35cd6ee09d9b08702446e6 Mon Sep 17 00:00:00 2001 From: Enric Pou Date: Wed, 28 May 2025 08:27:05 +0200 Subject: [PATCH 6/6] Add test_float_cast typing Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- Tests/test_tiff_ifdrational.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_tiff_ifdrational.py b/Tests/test_tiff_ifdrational.py index 1e42ff70e..8aae609a5 100644 --- a/Tests/test_tiff_ifdrational.py +++ b/Tests/test_tiff_ifdrational.py @@ -84,7 +84,7 @@ def test_ifd_rational_save( (1, 0, float("nan")), ], ) -def test_float_cast(numerator, denominator, expected_result): +def test_float_cast(numerator: int, denominator: int, expected_result: float) -> None: value = float(IFDRational(numerator, denominator)) if math.isnan(expected_result): assert value