mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-10 16:22:22 +03:00
Using math.info to treat 0 denominators for IFDRational
This commit is contained in:
parent
83de8cd22c
commit
025f3ba23a
|
@ -302,7 +302,10 @@ def test_tagtype_on_zero_denominator(
|
||||||
|
|
||||||
with Image.open(out) as reloaded:
|
with Image.open(out) as reloaded:
|
||||||
assert isinstance(reloaded, TiffImagePlugin.TiffImageFile)
|
assert isinstance(reloaded, TiffImagePlugin.TiffImageFile)
|
||||||
assert math.isnan(reloaded.tag_v2[37380])
|
if expected == TiffTags.RATIONAL:
|
||||||
|
assert reloaded.tag_v2[37380] == math.inf
|
||||||
|
elif TiffTags.SIGNED_RATIONAL:
|
||||||
|
assert reloaded.tag_v2[37380] == -math.inf
|
||||||
|
|
||||||
|
|
||||||
def test_undefined_zero(tmp_path: Path) -> None:
|
def test_undefined_zero(tmp_path: Path) -> None:
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from fractions import Fraction
|
from fractions import Fraction
|
||||||
|
import math
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -80,8 +81,14 @@ def test_ifd_rational_save(
|
||||||
"numerator, denominator, expected_result",
|
"numerator, denominator, expected_result",
|
||||||
[
|
[
|
||||||
(1, 1, 1.0),
|
(1, 1, 1.0),
|
||||||
(1, 0, float("nan")),
|
(1, 0, math.inf),
|
||||||
|
(-1, 0, -math.inf),
|
||||||
|
(0, 0, float("nan")),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_float_cast(numerator, denominator, expected_result):
|
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
|
||||||
|
|
|
@ -372,7 +372,12 @@ class IFDRational(Rational):
|
||||||
self._denominator = denominator
|
self._denominator = denominator
|
||||||
|
|
||||||
if denominator == 0:
|
if denominator == 0:
|
||||||
self._val = float("nan")
|
if value == 0:
|
||||||
|
self._val = float("nan")
|
||||||
|
elif value > 0:
|
||||||
|
self._val = math.inf
|
||||||
|
else:
|
||||||
|
self._val = -math.inf
|
||||||
elif denominator == 1:
|
elif denominator == 1:
|
||||||
self._val = Fraction(value)
|
self._val = Fraction(value)
|
||||||
elif int(value) == value:
|
elif int(value) == value:
|
||||||
|
@ -691,7 +696,7 @@ class ImageFileDirectory_v2(_IFDv2Base):
|
||||||
if all(isinstance(v, IFDRational) for v in values):
|
if all(isinstance(v, IFDRational) for v in values):
|
||||||
for v in values:
|
for v in values:
|
||||||
assert isinstance(v, IFDRational)
|
assert isinstance(v, IFDRational)
|
||||||
if v < 0 or (math.isnan(v) and float(v.numerator) < 0):
|
if v < 0:
|
||||||
self.tagtype[tag] = TiffTags.SIGNED_RATIONAL
|
self.tagtype[tag] = TiffTags.SIGNED_RATIONAL
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user