Skip to content

Commit 007974d

Browse files
committed
Ignore EXTRASAMPLES tag from separate planes image when saving
1 parent 84cb30d commit 007974d

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

Tests/test_file_libtiff.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,10 +1055,15 @@ def test_strip_planar_16bit_RGBa(self) -> None:
10551055
with Image.open("Tests/images/tiff_strip_planar_16bit_RGBa.tiff") as im:
10561056
assert_image_equal_tofile(im, "Tests/images/tiff_16bit_RGBa_target.png")
10571057

1058-
def test_separate_planar_extra_samples(self) -> None:
1058+
def test_separate_planar_extra_samples(self, tmp_path: Path) -> None:
1059+
out = tmp_path / "temp.tif"
10591060
with Image.open("Tests/images/separate_planar_extra_samples.tiff") as im:
10601061
assert im.mode == "L"
10611062

1063+
im.save(out)
1064+
with Image.open(out) as reloaded:
1065+
assert reloaded.mode == "L"
1066+
10621067
@pytest.mark.parametrize("compression", (None, "jpeg"))
10631068
def test_block_tile_tags(self, compression: str | None, tmp_path: Path) -> None:
10641069
im = hopper()

src/PIL/TiffImagePlugin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,12 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
17681768
legacy_ifd = im.tag.to_v2()
17691769

17701770
supplied_tags = {**legacy_ifd, **getattr(im, "tag_v2", {})}
1771+
if supplied_tags.get(PLANAR_CONFIGURATION) == 2 and EXTRASAMPLES in supplied_tags:
1772+
# If the image used separate component planes,
1773+
# then EXTRASAMPLES should be ignored when saving contiguously
1774+
if SAMPLESPERPIXEL in supplied_tags:
1775+
supplied_tags[SAMPLESPERPIXEL] -= len(supplied_tags[EXTRASAMPLES])
1776+
del supplied_tags[EXTRASAMPLES]
17711777
for tag in (
17721778
# IFD offset that may not be correct in the saved image
17731779
EXIFIFD,

0 commit comments

Comments
 (0)