Ignore EXTRASAMPLES tag from separate planes image when saving

This commit is contained in:
Andrew Murray 2026-03-30 20:04:39 +11:00
parent 84cb30d7a7
commit 007974d35b
2 changed files with 12 additions and 1 deletions

View File

@ -1055,10 +1055,15 @@ class TestFileLibTiff(LibTiffTestCase):
with Image.open("Tests/images/tiff_strip_planar_16bit_RGBa.tiff") as im:
assert_image_equal_tofile(im, "Tests/images/tiff_16bit_RGBa_target.png")
def test_separate_planar_extra_samples(self) -> None:
def test_separate_planar_extra_samples(self, tmp_path: Path) -> None:
out = tmp_path / "temp.tif"
with Image.open("Tests/images/separate_planar_extra_samples.tiff") as im:
assert im.mode == "L"
im.save(out)
with Image.open(out) as reloaded:
assert reloaded.mode == "L"
@pytest.mark.parametrize("compression", (None, "jpeg"))
def test_block_tile_tags(self, compression: str | None, tmp_path: Path) -> None:
im = hopper()

View File

@ -1768,6 +1768,12 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
legacy_ifd = im.tag.to_v2()
supplied_tags = {**legacy_ifd, **getattr(im, "tag_v2", {})}
if supplied_tags.get(PLANAR_CONFIGURATION) == 2 and EXTRASAMPLES in supplied_tags:
# If the image used separate component planes,
# then EXTRASAMPLES should be ignored when saving contiguously
if SAMPLESPERPIXEL in supplied_tags:
supplied_tags[SAMPLESPERPIXEL] -= len(supplied_tags[EXTRASAMPLES])
del supplied_tags[EXTRASAMPLES]
for tag in (
# IFD offset that may not be correct in the saved image
EXIFIFD,