Pillow/Tests/test_tiff_ifdrational.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

69 lines
1.6 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
from fractions import Fraction
from PIL import Image, TiffImagePlugin, features
from PIL.TiffImagePlugin import IFDRational
2020-02-25 12:57:27 +03:00
from .helper import hopper
2020-02-25 12:57:27 +03:00
def _test_equal(num, denom, target):
t = IFDRational(num, denom)
2020-02-25 12:57:27 +03:00
assert target == t
assert t == target
2016-01-24 04:44:31 +03:00
2020-02-25 12:57:27 +03:00
def test_sanity():
_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)
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
_test_equal(7, 5, 1.4)
2020-07-24 17:17:49 +03:00
def test_ranges():
for num in range(1, 10):
for denom in range(1, 10):
assert IFDRational(num, denom) == IFDRational(num, denom)
2020-02-25 12:57:27 +03:00
def test_nonetype():
# Fails if the _delegate function doesn't return a valid function
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
2020-02-25 12:57:27 +03:00
assert xres and 1
assert xres and yres
2016-01-24 04:44:31 +03:00
2020-02-25 12:57:27 +03:00
def test_ifd_rational_save(tmp_path):
methods = (True, False)
if not features.check("libtiff"):
methods = (False,)
for libtiff in methods:
TiffImagePlugin.WRITE_LIBTIFF = libtiff
im = hopper()
out = str(tmp_path / "temp.tiff")
res = IFDRational(301, 1)
im.save(out, dpi=(res, res), compression="raw")
with Image.open(out) as reloaded:
assert float(IFDRational(301, 1)) == float(reloaded.tag_v2[282])