Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Tests/test_file_tiff_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these to @pytest.mark.parametrize?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, done

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):
Expand Down
2 changes: 2 additions & 0 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down