Skip to content

Commit

Permalink
Merge pull request #1 from radarhere/exif-writing-fixes
Browse files Browse the repository at this point in the history
Corrected short and long range checks
  • Loading branch information
kkopachev committed Aug 29, 2019
2 parents 524933a + ddcfd25 commit 47513c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
12 changes: 12 additions & 0 deletions Tests/test_file_tiff_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,18 @@ def test_ifd_signed_rational(self):
self.assertEqual(numerator, reloaded.tag_v2[37380].numerator)
self.assertEqual(denominator, reloaded.tag_v2[37380].denominator)

def test_ifd_signed_long(self):
im = hopper()
info = TiffImagePlugin.ImageFileDirectory_v2()

info[37000] = -60000

out = self.tempfile("temp.tiff")
im.save(out, tiffinfo=info, compression="raw")

reloaded = Image.open(out)
self.assertEqual(reloaded.tag_v2[37000], -60000)

def test_expty_values(self):
data = io.BytesIO(
b"II*\x00\x08\x00\x00\x00\x03\x00\x1a\x01\x05\x00\x00\x00\x00\x00"
Expand Down
10 changes: 4 additions & 6 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,10 @@ def _setitem(self, tag, value, legacy_api):
else TiffTags.SIGNED_RATIONAL
)
elif all(isinstance(v, int) for v in values):
if all(v < 2 ** 16 for v in values):
self.tagtype[tag] = (
TiffTags.SHORT
if all(v >= 0 for v in values)
else TiffTags.SIGNED_SHORT
)
if all(0 <= v < 2 ** 16 for v in values):
self.tagtype[tag] = TiffTags.SHORT
elif all(-2 ** 15 < v < 2 ** 15 for v in values):
self.tagtype[tag] = TiffTags.SIGNED_SHORT
else:
self.tagtype[tag] = (
TiffTags.LONG
Expand Down

0 comments on commit 47513c5

Please sign in to comment.