From 61fb1c5bd4215a2cff2ec80578f901f71d073919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Pedersen?= Date: Mon, 23 Feb 2015 09:46:35 +0100 Subject: [PATCH 1/6] Tiff: allow writing floating poitn tag values Use the inverse logic used for loading floating point tag values to also write them out again. --- PIL/TiffImagePlugin.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index a533c27ea..d7291e87c 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -517,6 +517,15 @@ class ImageFileDirectory(collections.MutableMapping): elif typ == 7: # untyped data data = value = b"".join(value) + elif typ in (11, 12): + # float value + tmap = {11: 'f', 12: 'd'} + if not isinstance(value, tuple): + value = (value,) + a = array.array(tmap[typ], value) + if self.prefix != native_prefix: + a.byteswap() + data = a.tostring() elif isStringType(value[0]): # string data if isinstance(value, tuple): From 31be9f12ad096e4655a4415c2cfd2511229cd15b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Pedersen?= Date: Mon, 23 Feb 2015 10:51:42 +0100 Subject: [PATCH 2/6] Add tests for tiff float tag values Add tests for writing of float/double values in tiff file tags. --- Tests/test_file_tiff_metadata.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index 314590006..1cc5721e4 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -16,10 +16,18 @@ class TestFileTiffMetadata(PillowTestCase): img = hopper() textdata = "This is some arbitrary metadata for a text field" + floatdata = 12.345 + doubledata = 67.89 + info = TiffImagePlugin.ImageFileDirectory() info[tag_ids['ImageJMetaDataByteCounts']] = len(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 f = self.tempfile("temp.tif") @@ -29,6 +37,8 @@ class TestFileTiffMetadata(PillowTestCase): self.assertEqual(loaded.tag[50838], (len(textdata),)) self.assertEqual(loaded.tag[50839], textdata) + self.assertEqual(loaded.tag[tag_ids['RollAngle']], floatdata) + self.assertEqual(loaded.tag[tag_ids['YawAngle']], doubledata) def test_read_metadata(self): img = Image.open('Tests/images/hopper_g4.tif') From 1f11fc7be5dce094275f0efc78cf7fe6f57fcff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Pedersen?= Date: Mon, 23 Feb 2015 11:09:01 +0100 Subject: [PATCH 3/6] Fix typo in test --- Tests/test_file_tiff_metadata.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index 1cc5721e4..eb596f658 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -26,7 +26,7 @@ class TestFileTiffMetadata(PillowTestCase): info[tag_ids['RollAngle']] = floatdata info.tagtype[tag_ids['RollAngle']] = 11 - info[tag_ids['YawAngle'] = doubledata + info[tag_ids['YawAngle']] = doubledata info.tagtype[tag_ids['YawAngle']] = 12 f = self.tempfile("temp.tif") From 0238a80e249ac0786f9933b78180311825daa973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Pedersen?= Date: Mon, 23 Feb 2015 11:19:32 +0100 Subject: [PATCH 4/6] Fix test failure on float due to rounding, the values are just 'almost' equal. --- Tests/test_file_tiff_metadata.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index eb596f658..12689ba1c 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -37,8 +37,8 @@ class TestFileTiffMetadata(PillowTestCase): self.assertEqual(loaded.tag[50838], (len(textdata),)) self.assertEqual(loaded.tag[50839], textdata) - self.assertEqual(loaded.tag[tag_ids['RollAngle']], floatdata) - self.assertEqual(loaded.tag[tag_ids['YawAngle']], doubledata) + self.assertAlmostEqual(loaded.tag[tag_ids['RollAngle']], floatdata) + self.assertAlmostEqual(loaded.tag[tag_ids['YawAngle']], doubledata) def test_read_metadata(self): img = Image.open('Tests/images/hopper_g4.tif') From e8553e1dae56a4e5acc19c9df348a55b5fdbe5c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Pedersen?= Date: Mon, 23 Feb 2015 12:06:29 +0100 Subject: [PATCH 5/6] Tiff float tags: Use first val in tuple --- Tests/test_file_tiff_metadata.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index 12689ba1c..4642780c2 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -37,8 +37,8 @@ class TestFileTiffMetadata(PillowTestCase): self.assertEqual(loaded.tag[50838], (len(textdata),)) self.assertEqual(loaded.tag[50839], textdata) - self.assertAlmostEqual(loaded.tag[tag_ids['RollAngle']], floatdata) - self.assertAlmostEqual(loaded.tag[tag_ids['YawAngle']], doubledata) + self.assertAlmostEqual(loaded.tag[tag_ids['RollAngle']][0], floatdata) + self.assertAlmostEqual(loaded.tag[tag_ids['YawAngle']][0], doubledata) def test_read_metadata(self): img = Image.open('Tests/images/hopper_g4.tif') From 47f5c9e650bf73dcc5a096d8bc931e74bd0b8060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Pedersen?= Date: Mon, 23 Feb 2015 12:11:20 +0100 Subject: [PATCH 6/6] Fix float precission --- Tests/test_file_tiff_metadata.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index 4642780c2..2954c0879 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -37,7 +37,8 @@ class TestFileTiffMetadata(PillowTestCase): self.assertEqual(loaded.tag[50838], (len(textdata),)) self.assertEqual(loaded.tag[50839], textdata) - self.assertAlmostEqual(loaded.tag[tag_ids['RollAngle']][0], floatdata) + self.assertAlmostEqual(loaded.tag[tag_ids['RollAngle']][0], floatdata, + places=5) self.assertAlmostEqual(loaded.tag[tag_ids['YawAngle']][0], doubledata) def test_read_metadata(self):