'Test writing arbitrary metadata into the tiff image directory
Use case is ImageJ private tags, one numeric, one arbitrary
data.  https://github.com/python-pillow/Pillow/issues/291'

def test_rt_metadata(self):

    img = hopper()
    basetextdata = 'This    is    some    arbitrary    metadata    for    a    text    field'
    bindata = (basetextdata.encode('ascii') + '    \xff')
    textdata = ((basetextdata + '    ') + chr(255))
    reloaded_textdata = (basetextdata + '    ?')
    floatdata = 12.345
    doubledata = 67.89
    info = TiffImagePlugin.ImageFileDirectory()
    ImageJMetaData = tag_ids['ImageJMetaData']
    ImageJMetaDataByteCounts = tag_ids['ImageJMetaDataByteCounts']
    ImageDescription = tag_ids['ImageDescription']
    info[ImageJMetaDataByteCounts] = len(bindata)
    info[ImageJMetaData] = bindata
    info[tag_ids['RollAngle']] = floatdata
    info.tagtype[tag_ids['RollAngle']] = 11
    info[tag_ids['YawAngle']] = doubledata
    info.tagtype[tag_ids['YawAngle']] = 12
    info[ImageDescription] = textdata
    f = self.tempfile('temp.tif')
    img.save(f, tiffinfo=info)
    loaded = Image.open(f)
    self.assertEqual(loaded.tag[ImageJMetaDataByteCounts], (len(bindata),))
    self.assertEqual(loaded.tag_v2[ImageJMetaDataByteCounts], len(bindata))
    self.assertEqual(loaded.tag[ImageJMetaData], bindata)
    self.assertEqual(loaded.tag_v2[ImageJMetaData], bindata)
    self.assertEqual(loaded.tag[ImageDescription], (reloaded_textdata,))
    self.assertEqual(loaded.tag_v2[ImageDescription], reloaded_textdata)
    loaded_float = loaded.tag[tag_ids['RollAngle']][0]
    self.assertAlmostEqual(loaded_float, floatdata, places=5)
    loaded_double = loaded.tag[tag_ids['YawAngle']][0]
    self.assertAlmostEqual(loaded_double, doubledata)
