2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2020-12-12 06:12:30 +03:00
|
|
|
from PIL import Image, ImagePalette
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2021-07-10 16:13:46 +03:00
|
|
|
from .helper import assert_image_equal, assert_image_equal_tofile, hopper
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_putpalette() -> None:
|
2024-02-12 01:28:53 +03:00
|
|
|
def palette(mode: str) -> str | tuple[str, list[int]]:
|
2019-11-04 01:18:55 +03:00
|
|
|
im = hopper(mode).copy()
|
|
|
|
im.putpalette(list(range(256)) * 3)
|
|
|
|
p = im.getpalette()
|
|
|
|
if p:
|
|
|
|
return im.mode, p[:10]
|
|
|
|
return im.mode
|
2019-06-13 18:54:24 +03:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
palette("1")
|
|
|
|
for mode in ["L", "LA", "P", "PA"]:
|
|
|
|
assert palette(mode) == (
|
|
|
|
"PA" if "A" in mode else "P",
|
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
|
|
)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
palette("I")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
palette("F")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
palette("RGB")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
palette("RGBA")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
palette("YCbCr")
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2023-05-26 12:14:56 +03:00
|
|
|
with Image.open("Tests/images/hopper_gray.jpg") as im:
|
|
|
|
assert im.mode == "L"
|
|
|
|
im.putpalette(list(range(256)) * 3)
|
|
|
|
|
|
|
|
with Image.open("Tests/images/la.tga") as im:
|
|
|
|
assert im.mode == "LA"
|
|
|
|
im.putpalette(list(range(256)) * 3)
|
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_imagepalette() -> None:
|
2019-11-04 01:18:55 +03:00
|
|
|
im = hopper("P")
|
|
|
|
im.putpalette(ImagePalette.negative())
|
2021-07-10 16:13:46 +03:00
|
|
|
assert_image_equal_tofile(im.convert("RGB"), "Tests/images/palette_negative.png")
|
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
im.putpalette(ImagePalette.random())
|
2021-07-10 16:13:46 +03:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
im.putpalette(ImagePalette.sepia())
|
2021-07-10 16:13:46 +03:00
|
|
|
assert_image_equal_tofile(im.convert("RGB"), "Tests/images/palette_sepia.png")
|
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
im.putpalette(ImagePalette.wedge())
|
2021-07-10 16:13:46 +03:00
|
|
|
assert_image_equal_tofile(im.convert("RGB"), "Tests/images/palette_wedge.png")
|
2020-12-12 06:12:30 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_putpalette_with_alpha_values() -> None:
|
2020-12-12 06:12:30 +03:00
|
|
|
with Image.open("Tests/images/transparent.gif") as im:
|
|
|
|
expected = im.convert("RGBA")
|
|
|
|
|
|
|
|
palette = im.getpalette()
|
|
|
|
transparency = im.info.pop("transparency")
|
|
|
|
|
|
|
|
palette_with_alpha_values = []
|
|
|
|
for i in range(256):
|
|
|
|
color = palette[i * 3 : i * 3 + 3]
|
|
|
|
alpha = 0 if i == transparency else 255
|
|
|
|
palette_with_alpha_values += color + [alpha]
|
|
|
|
im.putpalette(palette_with_alpha_values, "RGBA")
|
|
|
|
|
|
|
|
assert_image_equal(im.convert("RGBA"), expected)
|
2022-02-14 12:28:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"mode, palette",
|
|
|
|
(
|
|
|
|
("RGBA", (1, 2, 3, 4)),
|
|
|
|
("RGBAX", (1, 2, 3, 4, 0)),
|
2024-05-22 13:05:07 +03:00
|
|
|
("ARGB", (4, 1, 2, 3)),
|
2022-02-14 12:28:47 +03:00
|
|
|
),
|
|
|
|
)
|
2024-02-12 01:28:53 +03:00
|
|
|
def test_rgba_palette(mode: str, palette: tuple[int, ...]) -> None:
|
2022-02-14 12:28:47 +03:00
|
|
|
im = Image.new("P", (1, 1))
|
|
|
|
im.putpalette(palette, mode)
|
2022-02-16 01:56:13 +03:00
|
|
|
assert im.getpalette() == [1, 2, 3]
|
2022-02-14 12:28:47 +03:00
|
|
|
assert im.palette.colors == {(1, 2, 3, 4): 0}
|
2023-07-17 02:26:24 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_empty_palette() -> None:
|
2023-07-17 16:04:43 +03:00
|
|
|
im = Image.new("P", (1, 1))
|
|
|
|
assert im.getpalette() == []
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_undefined_palette_index() -> None:
|
2023-07-17 02:26:24 +03:00
|
|
|
im = Image.new("P", (1, 1), 3)
|
|
|
|
im.putpalette((1, 2, 3))
|
|
|
|
assert im.convert("RGB").getpixel((0, 0)) == (0, 0, 0)
|