In a twisty maze of bytes, text and arbitrary metadata, py2 and py3. New IFD is putting textdata in type7 metadata and returning bytes, old one put it in type 2 string and returned a string. This may be an issue

This commit is contained in:
wiredfool 2015-09-11 23:44:23 +01:00
parent 426c9d8fc2
commit b1fdff4034
2 changed files with 10 additions and 7 deletions

View File

@ -361,7 +361,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
self.tagtype[tag] = 2
if self.tagtype[tag] == 7 and bytes is not str:
values = [value.encode("ascii") if isinstance(value, str) else value
values = [value.encode("ascii",'replace') if isinstance(value, str) else value
for value in values]
values = tuple(info.cvt_enum(value) for value in values)
if info.length == 1:

View File

@ -18,29 +18,32 @@ class TestFileTiffMetadata(PillowTestCase):
img = hopper()
basetextdata = "This is some arbitrary metadata for a text field"
textdata = basetextdata + " \xff"
textdata = basetextdata + " \xff"
reloaded_textdata = basetextdata.encode('ascii') + b" ?"
floatdata = 12.345
doubledata = 67.89
info = TiffImagePlugin.ImageFileDirectory()
info[tag_ids['ImageJMetaDataByteCounts']] = len(textdata)
info[tag_ids['ImageJMetaDataByteCounts']] = len(reloaded_textdata)
info[tag_ids['ImageJMetaData']] = textdata
info[tag_ids['RollAngle']] = floatdata
info.tagtype[tag_ids['RollAngle']] = 11
info[tag_ids['YawAngle']] = doubledata
info.tagtype[tag_ids['YawAngle']] = 12
print(info.tagtype)
f = self.tempfile("temp.tif")
img.save(f, tiffinfo=info)
loaded = Image.open(f)
self.assertEqual(loaded.tag[50838], (len(basetextdata + " ?"),))
self.assertEqual(loaded.tag_v2[50838], len(basetextdata + " ?"))
self.assertEqual(loaded.tag[50838], (len(reloaded_textdata),))
self.assertEqual(loaded.tag_v2[50838], len(reloaded_textdata))
self.assertEqual(loaded.tag[50839], basetextdata + " ?")
self.assertEqual(loaded.tag_v2[50839], basetextdata + " ?")
self.assertEqual(loaded.tag[50839], reloaded_textdata)
self.assertEqual(loaded.tag_v2[50839], reloaded_textdata)
loaded_float = loaded.tag[tag_ids['RollAngle']][0]