Skip to content

Commit 6697de1

Browse files
authored
Merge pull request #6493 from radarhere/bytes_ascii
2 parents 196210b + 3459120 commit 6697de1

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Tests/test_file_tiff_metadata.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@ def test_iptc(tmp_path):
185185
im.save(out)
186186

187187

188+
def test_writing_bytes_to_ascii(tmp_path):
189+
im = hopper()
190+
info = TiffImagePlugin.ImageFileDirectory_v2()
191+
192+
tag = TiffTags.TAGS_V2[271]
193+
assert tag.type == TiffTags.ASCII
194+
195+
info[271] = b"test"
196+
197+
out = str(tmp_path / "temp.tiff")
198+
im.save(out, tiffinfo=info)
199+
200+
with Image.open(out) as reloaded:
201+
assert reloaded.tag_v2[271] == "test"
202+
203+
188204
def test_undefined_zero(tmp_path):
189205
# Check that the tag has not been changed since this test was created
190206
tag = TiffTags.TAGS_V2[45059]

src/PIL/TiffImagePlugin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,9 @@ def load_string(self, data, legacy_api=True):
727727
@_register_writer(2)
728728
def write_string(self, value):
729729
# remerge of https://github.com/python-pillow/Pillow/pull/1416
730-
return b"" + value.encode("ascii", "replace") + b"\0"
730+
if not isinstance(value, bytes):
731+
value = value.encode("ascii", "replace")
732+
return value + b"\0"
731733

732734
@_register_loader(5, 8)
733735
def load_rational(self, data, legacy_api=True):

0 commit comments

Comments
 (0)