Skip to content

Commit c874256

Browse files
committed
Support saving variable length rational TIFF tags by default
1 parent fbdf607 commit c874256

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

Tests/test_file_libtiff.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,15 @@ 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+
out = tmp_path / "temp2.tif"
372+
with Image.open("Tests/images/rdf.tif") as im:
373+
im.save(out)
374+
375+
with Image.open(out) as reloaded:
376+
assert isinstance(reloaded, TiffImagePlugin.TiffImageFile)
377+
assert reloaded.tag_v2[318] == pytest.approx((0.3127, 0.3289999))
378+
370379
def test_xmlpacket_tag(
371380
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
372381
) -> None:

src/PIL/TiffImagePlugin.py

Lines changed: 7 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"),
@@ -1177,6 +1178,7 @@ def _open(self) -> None:
11771178
"""Open the first image in a TIFF file"""
11781179

11791180
# Header
1181+
assert self.fp is not None
11801182
ifh = self.fp.read(8)
11811183
if ifh[2] == 43:
11821184
ifh += self.fp.read(8)
@@ -1343,6 +1345,7 @@ def _load_libtiff(self) -> Image.core.PixelAccess | None:
13431345
# To be nice on memory footprint, if there's a
13441346
# file descriptor, use that instead of reading
13451347
# into a string in python.
1348+
assert self.fp is not None
13461349
try:
13471350
fp = hasattr(self.fp, "fileno") and self.fp.fileno()
13481351
# flush the file descriptor, prevents error on pypy 2.4+
@@ -1936,9 +1939,10 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
19361939
types[tag] = TiffTags.LONG8
19371940
elif tag in ifd.tagtype:
19381941
types[tag] = ifd.tagtype[tag]
1939-
elif not (isinstance(value, (int, float, str, bytes))):
1940-
continue
1941-
else:
1942+
elif isinstance(value, (int, float, str, bytes)) or (
1943+
isinstance(value, tuple)
1944+
and all(isinstance(v, (int, float, IFDRational)) for v in value)
1945+
):
19421946
type = TiffTags.lookup(tag).type
19431947
if type:
19441948
types[tag] = type

0 commit comments

Comments
 (0)