forked from python-pillow/Pillow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_file_webp_alpha.py
149 lines (110 loc) · 4.07 KB
/
test_file_webp_alpha.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import pytest
from PIL import Image
from .helper import (
assert_image_equal,
assert_image_similar,
assert_image_similar_tofile,
hopper,
)
_webp = pytest.importorskip("PIL._webp", reason="WebP support not installed")
def setup_module():
if _webp.WebPDecoderBuggyAlpha():
pytest.skip("Buggy early version of WebP installed, not testing transparency")
def test_read_rgba():
"""
Can we read an RGBA mode file without error?
Does it have the bits we expect?
"""
# Generated with `cwebp transparent.png -o transparent.webp`
file_path = "Tests/images/transparent.webp"
with Image.open(file_path) as image:
assert image.mode == "RGBA"
assert image.size == (200, 150)
assert image.format == "WEBP"
image.load()
image.getdata()
image.tobytes()
assert_image_similar_tofile(image, "Tests/images/transparent.png", 20.0)
def test_write_lossless_rgb(tmp_path):
"""
Can we write an RGBA mode file with lossless compression without error?
Does it have the bits we expect?
"""
temp_file = str(tmp_path / "temp.webp")
# temp_file = "temp.webp"
pil_image = hopper("RGBA")
mask = Image.new("RGBA", (64, 64), (128, 128, 128, 128))
# Add some partially transparent bits:
pil_image.paste(mask, (0, 0), mask)
pil_image.save(temp_file, lossless=True)
with Image.open(temp_file) as image:
image.load()
assert image.mode == "RGBA"
assert image.size == pil_image.size
assert image.format == "WEBP"
image.load()
image.getdata()
assert_image_equal(image, pil_image)
def test_write_rgba(tmp_path):
"""
Can we write a RGBA mode file to WebP without error.
Does it have the bits we expect?
"""
temp_file = str(tmp_path / "temp.webp")
pil_image = Image.new("RGBA", (10, 10), (255, 0, 0, 20))
pil_image.save(temp_file)
if _webp.WebPDecoderBuggyAlpha():
return
with Image.open(temp_file) as image:
image.load()
assert image.mode == "RGBA"
assert image.size == (10, 10)
assert image.format == "WEBP"
image.load()
image.getdata()
# Early versions of WebP are known to produce higher deviations:
# deal with it
if _webp.WebPDecoderVersion() <= 0x201:
assert_image_similar(image, pil_image, 3.0)
else:
assert_image_similar(image, pil_image, 1.0)
def test_keep_rgb_values_when_transparent(tmp_path):
"""
Saving transparent pixels should retain their original RGB values
when using the "exact" parameter.
"""
image = hopper("RGB")
# create a copy of the image
# with the left half transparent
half_transparent_image = image.copy()
new_alpha = Image.new("L", (128, 128), 255)
new_alpha.paste(0, (0, 0, 64, 128))
half_transparent_image.putalpha(new_alpha)
# save with transparent area preserved
temp_file = str(tmp_path / "temp.webp")
half_transparent_image.save(temp_file, exact=True, lossless=True)
with Image.open(temp_file) as reloaded:
assert reloaded.mode == "RGBA"
assert reloaded.format == "WEBP"
# even though it is lossless, if we don't use exact=True
# in libwebp >= 0.5, the transparent area will be filled with black
# (or something more conducive to compression)
assert_image_equal(reloaded.convert("RGB"), image)
def test_write_unsupported_mode_PA(tmp_path):
"""
Saving a palette-based file with transparency to WebP format
should work, and be similar to the original file.
"""
temp_file = str(tmp_path / "temp.webp")
file_path = "Tests/images/transparent.gif"
with Image.open(file_path) as im:
im.save(temp_file)
with Image.open(temp_file) as image:
assert image.mode == "RGBA"
assert image.size == (200, 150)
assert image.format == "WEBP"
image.load()
image.getdata()
with Image.open(file_path) as im:
target = im.convert("RGBA")
assert_image_similar(image, target, 25.0)