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
16 changes: 11 additions & 5 deletions Tests/test_imagepalette.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,23 @@ def test_make_gamma_lut() -> None:
assert lut[255] == 255


def test_rawmode_valueerrors(tmp_path: Path) -> None:
def test_raw() -> None:
data = list(range(256)) * 3
palette = ImagePalette.raw("RGB", data)
assert palette.tobytes() == bytes(data)


def test_raw_valueerrors(tmp_path: Path) -> None:
# Arrange
palette = ImagePalette.raw("RGB", list(range(256)) * 3)
palette = ImagePalette.raw("BGR", list(range(256)) * 3)

# Act / Assert
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="palette contains raw palette data"):
palette.tobytes()
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="palette contains raw palette data"):
palette.getcolor((1, 2, 3))
f = str(tmp_path / "temp.lut")
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="palette contains raw palette data"):
palette.save(f)


Expand Down
6 changes: 1 addition & 5 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2161,11 +2161,7 @@ def putpalette(
msg = "illegal image mode"
raise ValueError(msg)
if isinstance(data, ImagePalette.ImagePalette):
if data.rawmode is not None:
palette = ImagePalette.raw(data.rawmode, data.palette)
else:
palette = ImagePalette.ImagePalette(palette=data.palette)
palette.dirty = 1
palette = ImagePalette.raw(data.rawmode or "RGB", data.palette)
else:
if not isinstance(data, bytes):
data = bytes(data)
Expand Down
3 changes: 2 additions & 1 deletion src/PIL/ImagePalette.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ def save(self, fp: str | IO[str]) -> None:

def raw(rawmode: str, data: Sequence[int] | bytes | bytearray) -> ImagePalette:
palette = ImagePalette()
palette.rawmode = rawmode
if rawmode != "RGB":
palette.rawmode = rawmode
palette.palette = data
palette.dirty = 1
return palette
Expand Down