'Test metadata writing through libtiff'

def test_write_metadata(self):

    for legacy_api in [False, True]:
        img = Image.open('Tests/images/hopper_g4.tif')
        f = self.tempfile('temp.tiff')
        img.save(f, tiffinfo=img.tag)
        if legacy_api:
            original = img.tag.named()
        else:
            original = img.tag_v2.named()
        ignored = ['StripByteCounts', 'RowsPerStrip', 'PageNumber', 'PhotometricInterpretation']
        loaded = Image.open(f)
        if legacy_api:
            reloaded = loaded.tag.named()
        else:
            reloaded = loaded.tag_v2.named()
        for (tag, value) in itertools.chain(reloaded.items(), original.items()):
            if (tag not in ignored):
                val = original[tag]
                if tag.endswith('Resolution'):
                    if legacy_api:
                        self.assertEqual(c_float((val[0][0] / val[0][1])).value, c_float((value[0][0] / value[0][1])).value, msg=("%s    didn't    roundtrip" % tag))
                    else:
                        self.assertEqual(c_float(val).value, c_float(value).value, msg=("%s    didn't    roundtrip" % tag))
                else:
                    self.assertEqual(val, value, msg=("%s    didn't    roundtrip" % tag))
        requested_fields = ['StripByteCounts', 'RowsPerStrip', 'StripOffsets']
        for field in requested_fields:
            self.assertIn(field, reloaded, ('%s    not    in    metadata' % field))
