Pillow

Форк
0
/
test_file_webp_alpha.py 
152 строки · 4.2 Кб
1
from __future__ import annotations
2

3
from pathlib import Path
4

5
import pytest
6

7
from PIL import Image
8

9
from .helper import (
10
    assert_image_equal,
11
    assert_image_similar,
12
    assert_image_similar_tofile,
13
    hopper,
14
)
15

16
pytest.importorskip("PIL._webp", reason="WebP support not installed")
17

18

19
def test_read_rgba() -> None:
20
    """
21
    Can we read an RGBA mode file without error?
22
    Does it have the bits we expect?
23
    """
24

25
    # Generated with `cwebp transparent.png -o transparent.webp`
26
    file_path = "Tests/images/transparent.webp"
27
    with Image.open(file_path) as image:
28
        assert image.mode == "RGBA"
29
        assert image.size == (200, 150)
30
        assert image.format == "WEBP"
31
        image.load()
32
        image.getdata()
33

34
        image.tobytes()
35

36
        assert_image_similar_tofile(image, "Tests/images/transparent.png", 20.0)
37

38

39
def test_write_lossless_rgb(tmp_path: Path) -> None:
40
    """
41
    Can we write an RGBA mode file with lossless compression without error?
42
    Does it have the bits we expect?
43
    """
44

45
    temp_file = str(tmp_path / "temp.webp")
46
    # temp_file = "temp.webp"
47

48
    pil_image = hopper("RGBA")
49

50
    mask = Image.new("RGBA", (64, 64), (128, 128, 128, 128))
51
    # Add some partially transparent bits:
52
    pil_image.paste(mask, (0, 0), mask)
53

54
    pil_image.save(temp_file, lossless=True)
55

56
    with Image.open(temp_file) as image:
57
        image.load()
58

59
        assert image.mode == "RGBA"
60
        assert image.size == pil_image.size
61
        assert image.format == "WEBP"
62
        image.load()
63
        image.getdata()
64

65
        assert_image_equal(image, pil_image)
66

67

68
def test_write_rgba(tmp_path: Path) -> None:
69
    """
70
    Can we write a RGBA mode file to WebP without error.
71
    Does it have the bits we expect?
72
    """
73

74
    temp_file = str(tmp_path / "temp.webp")
75

76
    pil_image = Image.new("RGBA", (10, 10), (255, 0, 0, 20))
77
    pil_image.save(temp_file)
78

79
    with Image.open(temp_file) as image:
80
        image.load()
81

82
        assert image.mode == "RGBA"
83
        assert image.size == (10, 10)
84
        assert image.format == "WEBP"
85
        image.load()
86
        image.getdata()
87

88
        assert_image_similar(image, pil_image, 1.0)
89

90

91
def test_keep_rgb_values_when_transparent(tmp_path: Path) -> None:
92
    """
93
    Saving transparent pixels should retain their original RGB values
94
    when using the "exact" parameter.
95
    """
96

97
    image = hopper("RGB")
98

99
    # create a copy of the image
100
    # with the left half transparent
101
    half_transparent_image = image.copy()
102
    new_alpha = Image.new("L", (128, 128), 255)
103
    new_alpha.paste(0, (0, 0, 64, 128))
104
    half_transparent_image.putalpha(new_alpha)
105

106
    # save with transparent area preserved
107
    temp_file = str(tmp_path / "temp.webp")
108
    half_transparent_image.save(temp_file, exact=True, lossless=True)
109

110
    with Image.open(temp_file) as reloaded:
111
        assert reloaded.mode == "RGBA"
112
        assert reloaded.format == "WEBP"
113

114
        # even though it is lossless, if we don't use exact=True
115
        # in libwebp >= 0.5, the transparent area will be filled with black
116
        # (or something more conducive to compression)
117
        assert_image_equal(reloaded.convert("RGB"), image)
118

119

120
def test_write_unsupported_mode_PA(tmp_path: Path) -> None:
121
    """
122
    Saving a palette-based file with transparency to WebP format
123
    should work, and be similar to the original file.
124
    """
125

126
    temp_file = str(tmp_path / "temp.webp")
127
    file_path = "Tests/images/transparent.gif"
128
    with Image.open(file_path) as im:
129
        im.save(temp_file)
130
    with Image.open(temp_file) as image:
131
        assert image.mode == "RGBA"
132
        assert image.size == (200, 150)
133
        assert image.format == "WEBP"
134

135
        image.load()
136
        image.getdata()
137
        with Image.open(file_path) as im:
138
            target = im.convert("RGBA")
139

140
        assert_image_similar(image, target, 25.0)
141

142

143
def test_alpha_quality(tmp_path: Path) -> None:
144
    with Image.open("Tests/images/transparent.png") as im:
145
        out = str(tmp_path / "temp.webp")
146
        im.save(out)
147

148
        out_quality = str(tmp_path / "quality.webp")
149
        im.save(out_quality, alpha_quality=50)
150
        with Image.open(out) as reloaded:
151
            with Image.open(out_quality) as reloaded_quality:
152
                assert reloaded.tobytes() != reloaded_quality.tobytes()
153

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

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

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

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