Skip to content
Open
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
10 changes: 10 additions & 0 deletions Tests/test_file_bmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ def test_small_palette(tmp_path: Path) -> None:
assert reloaded.getpalette() == colors


def test_empty_palette(tmp_path: Path) -> None:
im = Image.new("P", (8, 8))

out = tmp_path / "temp.bmp"
im.save(out)

with Image.open(out) as reloaded:
assert_image_equal(im.convert("L"), reloaded)


def test_save_too_large(tmp_path: Path) -> None:
outfile = tmp_path / "temp.bmp"
with Image.new("RGB", (1, 1)) as im:
Expand Down
3 changes: 2 additions & 1 deletion src/PIL/BmpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ def _save(
elif im.mode == "L":
palette = b"".join(o8(i) * 3 + b"\x00" for i in range(256))
elif im.mode == "P":
palette = im.im.getpalette("RGB", "BGRX")
# Colors used should not be zero, as that is treated as the maximum
palette = im.im.getpalette("RGB", "BGRX") or b"\x00\x00\x00\x00"
colors = len(palette) // 4
else:
palette = None
Expand Down
Loading