From 6da4169f3724ffe20c72d8ef4a2e0dc21815b343 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 13 Dec 2022 22:40:55 +1100 Subject: [PATCH 1/2] Fixed writing int as ASCII tag --- Tests/test_file_tiff_metadata.py | 13 +++++++------ src/PIL/TiffImagePlugin.py | 2 ++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index b90dde3d96c..48c0273fe46 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -185,20 +185,21 @@ def test_iptc(tmp_path): im.save(out) -def test_writing_bytes_to_ascii(tmp_path): +def test_writing_other_types_to_ascii(tmp_path): im = hopper() info = TiffImagePlugin.ImageFileDirectory_v2() tag = TiffTags.TAGS_V2[271] assert tag.type == TiffTags.ASCII - info[271] = b"test" - out = str(tmp_path / "temp.tiff") - im.save(out, tiffinfo=info) + for (value, expected) in {b"test": "test", 1: "1"}.items(): + info[271] = value - with Image.open(out) as reloaded: - assert reloaded.tag_v2[271] == "test" + im.save(out, tiffinfo=info) + + with Image.open(out) as reloaded: + assert reloaded.tag_v2[271] == expected def test_writing_int_to_bytes(tmp_path): diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index ab9ac5ea23a..791e692c106 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -732,6 +732,8 @@ def load_string(self, data, legacy_api=True): @_register_writer(2) def write_string(self, value): # remerge of https://github.com/python-pillow/Pillow/pull/1416 + if isinstance(value, int): + value = str(value) if not isinstance(value, bytes): value = value.encode("ascii", "replace") return value + b"\0" From 0da8e43977f11837d9175419884d6a3295a7651e Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 29 Dec 2022 08:58:38 +1100 Subject: [PATCH 2/2] Parametrized test --- Tests/test_file_tiff_metadata.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index 48c0273fe46..48797ea084d 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -185,21 +185,21 @@ def test_iptc(tmp_path): im.save(out) -def test_writing_other_types_to_ascii(tmp_path): - im = hopper() +@pytest.mark.parametrize("value, expected", ((b"test", "test"), (1, "1"))) +def test_writing_other_types_to_ascii(value, expected, tmp_path): info = TiffImagePlugin.ImageFileDirectory_v2() tag = TiffTags.TAGS_V2[271] assert tag.type == TiffTags.ASCII - out = str(tmp_path / "temp.tiff") - for (value, expected) in {b"test": "test", 1: "1"}.items(): - info[271] = value + info[271] = value - im.save(out, tiffinfo=info) + im = hopper() + out = str(tmp_path / "temp.tiff") + im.save(out, tiffinfo=info) - with Image.open(out) as reloaded: - assert reloaded.tag_v2[271] == expected + with Image.open(out) as reloaded: + assert reloaded.tag_v2[271] == expected def test_writing_int_to_bytes(tmp_path):