Pillow

Форк
0
/
test_image_putpalette.py 
101 строка · 2.9 Кб
1
from __future__ import annotations
2

3
import pytest
4

5
from PIL import Image, ImagePalette
6

7
from .helper import assert_image_equal, assert_image_equal_tofile, hopper
8

9

10
def test_putpalette() -> None:
11
    def palette(mode: str) -> str | tuple[str, list[int]]:
12
        im = hopper(mode).copy()
13
        im.putpalette(list(range(256)) * 3)
14
        p = im.getpalette()
15
        if p:
16
            return im.mode, p[:10]
17
        return im.mode
18

19
    with pytest.raises(ValueError):
20
        palette("1")
21
    for mode in ["L", "LA", "P", "PA"]:
22
        assert palette(mode) == (
23
            "PA" if "A" in mode else "P",
24
            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
25
        )
26
    with pytest.raises(ValueError):
27
        palette("I")
28
    with pytest.raises(ValueError):
29
        palette("F")
30
    with pytest.raises(ValueError):
31
        palette("RGB")
32
    with pytest.raises(ValueError):
33
        palette("RGBA")
34
    with pytest.raises(ValueError):
35
        palette("YCbCr")
36

37
    with Image.open("Tests/images/hopper_gray.jpg") as im:
38
        assert im.mode == "L"
39
        im.putpalette(list(range(256)) * 3)
40

41
    with Image.open("Tests/images/la.tga") as im:
42
        assert im.mode == "LA"
43
        im.putpalette(list(range(256)) * 3)
44

45

46
def test_imagepalette() -> None:
47
    im = hopper("P")
48
    im.putpalette(ImagePalette.negative())
49
    assert_image_equal_tofile(im.convert("RGB"), "Tests/images/palette_negative.png")
50

51
    im.putpalette(ImagePalette.random())
52

53
    im.putpalette(ImagePalette.sepia())
54
    assert_image_equal_tofile(im.convert("RGB"), "Tests/images/palette_sepia.png")
55

56
    im.putpalette(ImagePalette.wedge())
57
    assert_image_equal_tofile(im.convert("RGB"), "Tests/images/palette_wedge.png")
58

59

60
def test_putpalette_with_alpha_values() -> None:
61
    with Image.open("Tests/images/transparent.gif") as im:
62
        expected = im.convert("RGBA")
63

64
        palette = im.getpalette()
65
        transparency = im.info.pop("transparency")
66

67
        palette_with_alpha_values = []
68
        for i in range(256):
69
            color = palette[i * 3 : i * 3 + 3]
70
            alpha = 0 if i == transparency else 255
71
            palette_with_alpha_values += color + [alpha]
72
        im.putpalette(palette_with_alpha_values, "RGBA")
73

74
        assert_image_equal(im.convert("RGBA"), expected)
75

76

77
@pytest.mark.parametrize(
78
    "mode, palette",
79
    (
80
        ("RGBA", (1, 2, 3, 4)),
81
        ("RGBAX", (1, 2, 3, 4, 0)),
82
        ("ARGB", (4, 1, 2, 3)),
83
    ),
84
)
85
def test_rgba_palette(mode: str, palette: tuple[int, ...]) -> None:
86
    im = Image.new("P", (1, 1))
87
    im.putpalette(palette, mode)
88
    assert im.getpalette() == [1, 2, 3]
89
    assert im.palette is not None
90
    assert im.palette.colors == {(1, 2, 3, 4): 0}
91

92

93
def test_empty_palette() -> None:
94
    im = Image.new("P", (1, 1))
95
    assert im.getpalette() == []
96

97

98
def test_undefined_palette_index() -> None:
99
    im = Image.new("P", (1, 1), 3)
100
    im.putpalette((1, 2, 3))
101
    assert im.convert("RGB").getpixel((0, 0)) == (0, 0, 0)
102

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.