Skip to content

Commit

Permalink
Merge pull request #7840 from radarhere/write_undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Mar 11, 2024
2 parents 9fec5d5 + 9637ea7 commit 4e92ee0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Tests/test_file_tiff_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,17 @@ def test_writing_other_types_to_bytes(value: int | IFDRational, tmp_path: Path)
assert reloaded.tag_v2[700] == b"\x01"


def test_writing_other_types_to_undefined(tmp_path: Path) -> None:
@pytest.mark.parametrize("value", (1, IFDRational(1)))
def test_writing_other_types_to_undefined(
value: int | IFDRational, tmp_path: Path
) -> None:
im = hopper()
info = TiffImagePlugin.ImageFileDirectory_v2()

tag = TiffTags.TAGS_V2[33723]
assert tag.type == TiffTags.UNDEFINED

info[33723] = 1
info[33723] = value

out = str(tmp_path / "temp.tiff")
im.save(out, tiffinfo=info)
Expand Down
2 changes: 2 additions & 0 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,8 @@ def load_undefined(self, data, legacy_api=True):

@_register_writer(7)
def write_undefined(self, value):
if isinstance(value, IFDRational):
value = int(value)
if isinstance(value, int):
value = str(value).encode("ascii", "replace")
return value
Expand Down

0 comments on commit 4e92ee0

Please sign in to comment.