Skip to content

Commit

Permalink
Merge pull request #6800 from radarhere/int_ascii
Browse files Browse the repository at this point in the history
Resolves #6799
  • Loading branch information
hugovk committed Dec 29, 2022
2 parents a7f8e86 + 0da8e43 commit 1ba19b1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Tests/test_file_tiff_metadata.py
Expand Up @@ -185,20 +185,21 @@ def test_iptc(tmp_path):
im.save(out)


def test_writing_bytes_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

info[271] = b"test"
info[271] = value

im = hopper()
out = str(tmp_path / "temp.tiff")
im.save(out, tiffinfo=info)

with Image.open(out) as reloaded:
assert reloaded.tag_v2[271] == "test"
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
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

0 comments on commit 1ba19b1

Please sign in to comment.