Skip to content

Commit 84cb30d

Browse files
committed
For separate planar configuration, ignore unspecified extra components
1 parent 07c180b commit 84cb30d

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

202 Bytes
Binary file not shown.

Tests/test_file_libtiff.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,10 @@ 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:
1059+
with Image.open("Tests/images/separate_planar_extra_samples.tiff") as im:
1060+
assert im.mode == "L"
1061+
10581062
@pytest.mark.parametrize("compression", (None, "jpeg"))
10591063
def test_block_tile_tags(self, compression: str | None, tmp_path: Path) -> None:
10601064
im = hopper()

src/PIL/TiffImagePlugin.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,18 +1483,24 @@ def _setup(self) -> None:
14831483

14841484
bps_tuple = self.tag_v2.get(BITSPERSAMPLE, (1,))
14851485
extra_tuple = self.tag_v2.get(EXTRASAMPLES, ())
1486+
samples_per_pixel = self.tag_v2.get(
1487+
SAMPLESPERPIXEL,
1488+
3 if self._compression == "tiff_jpeg" and photo in (2, 6) else 1,
1489+
)
14861490
if photo in (2, 6, 8): # RGB, YCbCr, LAB
14871491
bps_count = 3
14881492
elif photo == 5: # CMYK
14891493
bps_count = 4
14901494
else:
14911495
bps_count = 1
1496+
if self._planar_configuration == 2 and extra_tuple and max(extra_tuple) == 0:
1497+
# If components are stored separately,
1498+
# then unspecified extra components at the end can be ignored
1499+
bps_tuple = bps_tuple[: -len(extra_tuple)]
1500+
samples_per_pixel -= len(extra_tuple)
1501+
extra_tuple = ()
14921502
bps_count += len(extra_tuple)
14931503
bps_actual_count = len(bps_tuple)
1494-
samples_per_pixel = self.tag_v2.get(
1495-
SAMPLESPERPIXEL,
1496-
3 if self._compression == "tiff_jpeg" and photo in (2, 6) else 1,
1497-
)
14981504

14991505
if samples_per_pixel > MAX_SAMPLESPERPIXEL:
15001506
# DOS check, samples_per_pixel can be a Long, and we extend the tuple below

0 commit comments

Comments
 (0)