Skip to content

Commit 779fb49

Browse files
authored
Merge pull request #6270 from radarhere/samples_per_pixel
Adjust BITSPERSAMPLE to match SAMPLESPERPIXEL
2 parents 3ad3d2e + 4e52d06 commit 779fb49

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed
47.9 KB
Binary file not shown.

Tests/test_file_tiff.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,33 @@ def test_bigtiff(self):
9292
assert_image_equal_tofile(im, "Tests/images/hopper.tif")
9393

9494
@pytest.mark.parametrize(
95-
"file_name,mode,size,offset",
95+
"file_name,mode,size,tile",
9696
[
97-
("tiff_wrong_bits_per_sample.tiff", "RGBA", (52, 53), 160),
98-
("tiff_wrong_bits_per_sample_2.tiff", "RGB", (16, 16), 8),
97+
(
98+
"tiff_wrong_bits_per_sample.tiff",
99+
"RGBA",
100+
(52, 53),
101+
[("raw", (0, 0, 52, 53), 160, ("RGBA", 0, 1))],
102+
),
103+
(
104+
"tiff_wrong_bits_per_sample_2.tiff",
105+
"RGB",
106+
(16, 16),
107+
[("raw", (0, 0, 16, 16), 8, ("RGB", 0, 1))],
108+
),
109+
(
110+
"tiff_wrong_bits_per_sample_3.tiff",
111+
"RGBA",
112+
(512, 256),
113+
[("libtiff", (0, 0, 512, 256), 0, ("RGBA", "tiff_lzw", False, 48782))],
114+
),
99115
],
100116
)
101-
def test_wrong_bits_per_sample(self, file_name, mode, size, offset):
117+
def test_wrong_bits_per_sample(self, file_name, mode, size, tile):
102118
with Image.open("Tests/images/" + file_name) as im:
103119
assert im.mode == mode
104120
assert im.size == size
105-
assert im.tile == [("raw", (0, 0) + size, offset, (mode, 0, 1))]
121+
assert im.tile == tile
106122
im.load()
107123

108124
def test_set_legacy_api(self):

src/PIL/TiffImagePlugin.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,19 +1355,19 @@ def _setup(self):
13551355
bps_count = 1
13561356
bps_count += len(extra_tuple)
13571357
bps_actual_count = len(bps_tuple)
1358-
if bps_count < bps_actual_count:
1358+
samples_per_pixel = self.tag_v2.get(
1359+
SAMPLESPERPIXEL,
1360+
3 if self._compression == "tiff_jpeg" and photo in (2, 6) else 1,
1361+
)
1362+
if samples_per_pixel < bps_actual_count:
13591363
# If a file has more values in bps_tuple than expected,
13601364
# remove the excess.
1361-
bps_tuple = bps_tuple[:bps_count]
1362-
elif bps_count > bps_actual_count and bps_actual_count == 1:
1365+
bps_tuple = bps_tuple[:samples_per_pixel]
1366+
elif samples_per_pixel > bps_actual_count and bps_actual_count == 1:
13631367
# If a file has only one value in bps_tuple, when it should have more,
13641368
# presume it is the same number of bits for all of the samples.
1365-
bps_tuple = bps_tuple * bps_count
1369+
bps_tuple = bps_tuple * samples_per_pixel
13661370

1367-
samples_per_pixel = self.tag_v2.get(
1368-
SAMPLESPERPIXEL,
1369-
3 if self._compression == "tiff_jpeg" and photo in (2, 6) else 1,
1370-
)
13711371
if len(bps_tuple) != samples_per_pixel:
13721372
raise SyntaxError("unknown data organization")
13731373

0 commit comments

Comments
 (0)