Write round trip for rationals, including nan value

This commit is contained in:
wiredfool 2015-12-29 22:00:36 +00:00
parent 48e4e0722e
commit 3ac9396e8c
4 changed files with 36 additions and 5 deletions

View File

@ -497,11 +497,13 @@ class ImageFileDirectory_v2(collections.MutableMapping):
values = [value] if isinstance(value, basetypes) else value
if tag not in self.tagtype:
try:
if info.type:
self.tagtype[tag] = info.type
except KeyError:
else:
self.tagtype[tag] = 7
if all(isinstance(v, int) for v in values):
if all(isinstance(v, IFDRational) for v in values):
self.tagtype[tag] = 5
elif all(isinstance(v, int) for v in values):
if all(v < 2 ** 16 for v in values):
self.tagtype[tag] = 3
else:

View File

@ -23,7 +23,7 @@ from collections import namedtuple
class TagInfo(namedtuple("_TagInfo", "value name type length enum")):
__slots__ = []
def __new__(cls, value=None, name="unknown", type=4, length=0, enum=None):
def __new__(cls, value=None, name="unknown", type=None, length=0, enum=None):
return super(TagInfo, cls).__new__(
cls, value, name, type, length, enum or {})

View File

@ -185,6 +185,20 @@ class TestFileTiffMetadata(PillowTestCase):
self.assertEqual(im.tag_v2.tagtype[34675], 1)
self.assertTrue(im.info['icc_profile'])
def test_exif_div_zero(self):
im = hopper()
info = TiffImagePlugin.ImageFileDirectory_v2()
info[41988] = TiffImagePlugin.IFDRational(0,0)
out = self.tempfile('temp.tiff')
im.save(out, tiffinfo=info, compression='raw')
reloaded = Image.open(out)
self.assertEqual(0, reloaded.tag_v2[41988][0].numerator)
self.assertEqual(0, reloaded.tag_v2[41988][0].denominator)
if __name__ == '__main__':
unittest.main()

View File

@ -1,7 +1,8 @@
from __future__ import print_function
from helper import PillowTestCase
from helper import PillowTestCase, hopper
from PIL import TiffImagePlugin, Image
from PIL.TiffImagePlugin import IFDRational
from fractions import Fraction
@ -44,3 +45,17 @@ class Test_IFDRational(PillowTestCase):
self.assert_(xres and 1)
self.assert_(xres and yres)
def test_ifd_rational_save(self):
for libtiff in (True, False):
TiffImagePlugin.WRITE_LIBTIFF = libtiff
im = hopper()
out = self.tempfile('temp.tiff')
res = IFDRational(301,1)
im.save(out, dpi=(res,res), compression='raw')
reloaded = Image.open(out)
self.assertEqual(float(IFDRational(301,1)),
float(reloaded.tag_v2[282]))