2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from fractions import Fraction
|
2024-01-31 12:12:58 +03:00
|
|
|
from pathlib import Path
|
2015-11-18 19:51:57 +03:00
|
|
|
|
2024-04-14 14:57:29 +03:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from PIL import Image, TiffImagePlugin
|
2015-11-18 19:51:57 +03:00
|
|
|
from PIL.TiffImagePlugin import IFDRational
|
|
|
|
|
2024-04-14 14:57:29 +03:00
|
|
|
from .helper import hopper, skip_unless_feature
|
2015-11-18 19:51:57 +03:00
|
|
|
|
|
|
|
|
2024-07-05 20:56:24 +03:00
|
|
|
def _test_equal(
|
|
|
|
num: float | Fraction | IFDRational,
|
|
|
|
denom: int,
|
|
|
|
target: float | Fraction | IFDRational,
|
|
|
|
) -> None:
|
2020-02-25 12:57:27 +03:00
|
|
|
t = IFDRational(num, denom)
|
2015-11-18 19:51:57 +03:00
|
|
|
|
2020-02-25 12:57:27 +03:00
|
|
|
assert target == t
|
|
|
|
assert t == target
|
2016-01-24 04:44:31 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_sanity() -> None:
|
2020-02-25 12:57:27 +03:00
|
|
|
_test_equal(1, 1, 1)
|
|
|
|
_test_equal(1, 1, Fraction(1, 1))
|
2016-01-24 04:44:31 +03:00
|
|
|
|
2020-02-25 12:57:27 +03:00
|
|
|
_test_equal(2, 2, 1)
|
|
|
|
_test_equal(1.0, 1, Fraction(1, 1))
|
2016-01-24 04:44:31 +03:00
|
|
|
|
2020-02-25 12:57:27 +03:00
|
|
|
_test_equal(Fraction(1, 1), 1, Fraction(1, 1))
|
|
|
|
_test_equal(IFDRational(1, 1), 1, 1)
|
2015-11-18 19:51:57 +03:00
|
|
|
|
2020-02-25 12:57:27 +03:00
|
|
|
_test_equal(1, 2, Fraction(1, 2))
|
|
|
|
_test_equal(1, 2, IFDRational(1, 2))
|
2016-01-24 04:44:31 +03:00
|
|
|
|
2021-04-17 08:39:42 +03:00
|
|
|
_test_equal(7, 5, 1.4)
|
|
|
|
|
2015-11-18 19:51:57 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_ranges() -> None:
|
2020-07-24 17:17:49 +03:00
|
|
|
for num in range(1, 10):
|
|
|
|
for denom in range(1, 10):
|
|
|
|
assert IFDRational(num, denom) == IFDRational(num, denom)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_nonetype() -> None:
|
2020-02-25 12:57:27 +03:00
|
|
|
# Fails if the _delegate function doesn't return a valid function
|
2015-11-18 19:51:57 +03:00
|
|
|
|
2020-02-25 12:57:27 +03:00
|
|
|
xres = IFDRational(72)
|
|
|
|
yres = IFDRational(72)
|
|
|
|
assert xres._val is not None
|
|
|
|
assert xres.numerator is not None
|
|
|
|
assert xres.denominator is not None
|
|
|
|
assert yres._val is not None
|
2016-08-04 09:40:12 +03:00
|
|
|
|
2024-08-16 14:52:56 +03:00
|
|
|
assert xres
|
|
|
|
assert yres
|
2016-01-24 04:44:31 +03:00
|
|
|
|
2015-12-30 01:00:36 +03:00
|
|
|
|
2024-04-14 14:57:29 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"libtiff", (pytest.param(True, marks=skip_unless_feature("libtiff")), False)
|
|
|
|
)
|
2024-04-18 10:57:40 +03:00
|
|
|
def test_ifd_rational_save(
|
|
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, libtiff: bool
|
|
|
|
) -> None:
|
2024-04-14 14:57:29 +03:00
|
|
|
im = hopper()
|
|
|
|
out = str(tmp_path / "temp.tiff")
|
|
|
|
res = IFDRational(301, 1)
|
2020-02-25 12:57:27 +03:00
|
|
|
|
2024-04-18 10:57:40 +03:00
|
|
|
monkeypatch.setattr(TiffImagePlugin, "WRITE_LIBTIFF", libtiff)
|
2024-04-14 14:57:29 +03:00
|
|
|
im.save(out, dpi=(res, res), compression="raw")
|
2020-02-25 12:57:27 +03:00
|
|
|
|
2024-04-14 14:57:29 +03:00
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
assert float(IFDRational(301, 1)) == float(reloaded.tag_v2[282])
|