Fix for UnicodeDecodeError in TiffImagePlugin

Fix for UnicodeDecodeError: ascii codec cannot decode byte while saving a TIFF image

Problem occured while saving TIFF images that contain non-ascii characters in metadata

Manually merged with master by wiredfool
This commit is contained in:
Bogdan Kubala 2015-06-27 18:35:36 +02:00 committed by wiredfool
parent 525f47a64f
commit 42b5a85cb4
2 changed files with 6 additions and 3 deletions

View File

@ -550,6 +550,8 @@ class ImageFileDirectory(collections.MutableMapping):
# contains a 7-bit ASCII code; the last byte must be
# NUL (binary zero). Also, I don't think this was well
# exercised before.
if sys.version_info[0] == 2:
value = value.decode('ascii','replace')
data = value = b"" + value.encode('ascii', 'replace') + b"\0"
else:
# integer data

View File

@ -15,7 +15,8 @@ class TestFileTiffMetadata(PillowTestCase):
img = hopper()
textdata = "This is some arbitrary metadata for a text field"
basetextdata = "This is some arbitrary metadata for a text field"
textdata = basetextdata + " \xff"
floatdata = 12.345
doubledata = 67.89
@ -35,8 +36,8 @@ class TestFileTiffMetadata(PillowTestCase):
loaded = Image.open(f)
self.assertEqual(loaded.tag[50838], (len(textdata),))
self.assertEqual(loaded.tag[50839], textdata)
self.assertEqual(loaded.tag[50838], (len(basetextdata + " ?"),))
self.assertEqual(loaded.tag[50839], basetextdata + " ?")
self.assertAlmostEqual(loaded.tag[tag_ids['RollAngle']][0], floatdata,
places=5)
self.assertAlmostEqual(loaded.tag[tag_ids['YawAngle']][0], doubledata)