Skip to content

Commit

Permalink
Added state methods to allow pickling of IFDRational
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Apr 29, 2021
1 parent 537cd7a commit b78cf8c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Tests/test_file_tiff.py
Expand Up @@ -146,15 +146,17 @@ def test_load_float_dpi(self, resolutionUnit, dpi):
"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")
with Image.open("Tests/images/hopper.tif") as im:
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()
Expand Down
10 changes: 10 additions & 0 deletions src/PIL/TiffImagePlugin.py
Expand Up @@ -358,6 +358,16 @@ def __eq__(self, other):
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)
Expand Down

0 comments on commit b78cf8c

Please sign in to comment.