Skip to content

Commit 6e75d7a

Browse files
committed
Support saving variable length rational TIFF tags by default
1 parent 9fd4af5 commit 6e75d7a

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Tests/test_file_libtiff.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,14 @@ def test_whitepoint_tag(
367367
assert isinstance(reloaded, TiffImagePlugin.TiffImageFile)
368368
assert reloaded.tag_v2[318] == pytest.approx((0.3127, 0.3289))
369369

370+
# Save tag by default
371+
with Image.open("Tests/images/rdf.tif") as im:
372+
out = tmp_path / "temp2.tif"
373+
im.save(out)
374+
375+
with Image.open(out) as reloaded:
376+
assert reloaded.tag_v2[318] == pytest.approx((0.3127, 0.3289999))
377+
370378
def test_xmlpacket_tag(
371379
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
372380
) -> None:

src/PIL/TiffImagePlugin.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
(II, 3, (1,), 1, (8,), ()): ("P", "P"),
253253
(MM, 3, (1,), 1, (8,), ()): ("P", "P"),
254254
(II, 3, (1,), 1, (8, 8), (0,)): ("P", "PX"),
255+
(MM, 3, (1,), 1, (8, 8), (0,)): ("P", "PX"),
255256
(II, 3, (1,), 1, (8, 8), (2,)): ("PA", "PA"),
256257
(MM, 3, (1,), 1, (8, 8), (2,)): ("PA", "PA"),
257258
(II, 3, (1,), 2, (8,), ()): ("P", "P;R"),
@@ -1936,9 +1937,10 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
19361937
types[tag] = TiffTags.LONG8
19371938
elif tag in ifd.tagtype:
19381939
types[tag] = ifd.tagtype[tag]
1939-
elif not (isinstance(value, (int, float, str, bytes))):
1940-
continue
1941-
else:
1940+
elif isinstance(value, (int, float, str, bytes)) or (
1941+
isinstance(value, tuple)
1942+
and all(isinstance(v, (int, float, IFDRational)) for v in value)
1943+
):
19421944
type = TiffTags.lookup(tag).type
19431945
if type:
19441946
types[tag] = type

0 commit comments

Comments
 (0)