Added state methods to allow pickling of IFDRational

This commit is contained in:
Andrew Murray 2021-04-27 17:54:44 +10:00
parent 537cd7a82e
commit b78cf8cd75
2 changed files with 14 additions and 2 deletions

View File

@ -146,7 +146,8 @@ class TestFileTiff:
"Tests/images/hopper_float_dpi_" + str(resolutionUnit) + ".tif"
) as im:
assert im.tag_v2.get(RESOLUTION_UNIT) == resolutionUnit
assert im.info["dpi"] == (dpi, dpi)
for reloaded_dpi in im.info["dpi"]:
assert float(reloaded_dpi) == dpi
def test_save_float_dpi(self, tmp_path):
outfile = str(tmp_path / "temp.tif")
@ -154,7 +155,8 @@ class TestFileTiff:
im.save(outfile, dpi=(72.2, 72.2))
with Image.open(outfile) as reloaded:
assert reloaded.info["dpi"] == (72.2, 72.2)
for dpi in reloaded.info["dpi"]:
assert float(dpi) == 72.2
def test_save_setting_missing_resolution(self):
b = BytesIO()

View File

@ -358,6 +358,16 @@ class IFDRational(Rational):
other = other._val
return self._val == other
def __getstate__(self):
return [self._val, self._numerator, self._denominator]
def __setstate__(self, state):
IFDRational.__init__(self, 0)
_val, _numerator, _denominator = state
self._val = _val
self._numerator = _numerator
self._denominator = _denominator
def _delegate(op):
def delegate(self, *args):
return getattr(self._val, op)(*args)