Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Tests/images/tiff_wrong_bits_per_sample_2.tiff
Binary file not shown.
17 changes: 12 additions & 5 deletions Tests/test_file_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,18 @@ def test_mac_tiff(self):

assert_image_similar_tofile(im, "Tests/images/pil136.png", 1)

def test_wrong_bits_per_sample(self):
with Image.open("Tests/images/tiff_wrong_bits_per_sample.tiff") as im:
assert im.mode == "RGBA"
assert im.size == (52, 53)
assert im.tile == [("raw", (0, 0, 52, 53), 160, ("RGBA", 0, 1))]
@pytest.mark.parametrize(
"file_name,mode,w,h,offset",
[
("tiff_wrong_bits_per_sample.tiff", "RGBA", 52, 53, 160),
("tiff_wrong_bits_per_sample_2.tiff", "RGB", 16, 16, 8),
],
)
def test_wrong_bits_per_sample(self, file_name, mode, w, h, offset):
with Image.open("Tests/images/" + file_name) as im:
assert im.mode == mode
assert im.size == (w, h)
assert im.tile == [("raw", (0, 0, w, h), offset, (mode, 0, 1))]
im.load()

def test_set_legacy_api(self):
Expand Down
8 changes: 6 additions & 2 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,8 +1308,12 @@ def _setup(self):
bps_count = 1
bps_count += len(extra_tuple)
# Some files have only one value in bps_tuple,
# while should have more. Fix it
if bps_count > len(bps_tuple) and len(bps_tuple) == 1:
# while should have more. Or have more values
# than expected. Fix it
bps_actual_count = len(bps_tuple)
if bps_count < bps_actual_count:
bps_tuple = bps_tuple[:bps_count]
elif bps_count > bps_actual_count and bps_actual_count == 1:
bps_tuple = bps_tuple * bps_count

samplesPerPixel = self.tag_v2.get(
Expand Down