1
from __future__ import annotations
3
from pathlib import Path
12
assert_image_similar_tofile,
16
pytest.importorskip("PIL._webp", reason="WebP support not installed")
19
def test_read_rgba() -> None:
21
Can we read an RGBA mode file without error?
22
Does it have the bits we expect?
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"
36
assert_image_similar_tofile(image, "Tests/images/transparent.png", 20.0)
39
def test_write_lossless_rgb(tmp_path: Path) -> None:
41
Can we write an RGBA mode file with lossless compression without error?
42
Does it have the bits we expect?
45
temp_file = str(tmp_path / "temp.webp")
46
# temp_file = "temp.webp"
48
pil_image = hopper("RGBA")
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)
54
pil_image.save(temp_file, lossless=True)
56
with Image.open(temp_file) as image:
59
assert image.mode == "RGBA"
60
assert image.size == pil_image.size
61
assert image.format == "WEBP"
65
assert_image_equal(image, pil_image)
68
def test_write_rgba(tmp_path: Path) -> None:
70
Can we write a RGBA mode file to WebP without error.
71
Does it have the bits we expect?
74
temp_file = str(tmp_path / "temp.webp")
76
pil_image = Image.new("RGBA", (10, 10), (255, 0, 0, 20))
77
pil_image.save(temp_file)
79
with Image.open(temp_file) as image:
82
assert image.mode == "RGBA"
83
assert image.size == (10, 10)
84
assert image.format == "WEBP"
88
assert_image_similar(image, pil_image, 1.0)
91
def test_keep_rgb_values_when_transparent(tmp_path: Path) -> None:
93
Saving transparent pixels should retain their original RGB values
94
when using the "exact" parameter.
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)
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)
110
with Image.open(temp_file) as reloaded:
111
assert reloaded.mode == "RGBA"
112
assert reloaded.format == "WEBP"
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)
120
def test_write_unsupported_mode_PA(tmp_path: Path) -> None:
122
Saving a palette-based file with transparency to WebP format
123
should work, and be similar to the original file.
126
temp_file = str(tmp_path / "temp.webp")
127
file_path = "Tests/images/transparent.gif"
128
with Image.open(file_path) as im:
130
with Image.open(temp_file) as image:
131
assert image.mode == "RGBA"
132
assert image.size == (200, 150)
133
assert image.format == "WEBP"
137
with Image.open(file_path) as im:
138
target = im.convert("RGBA")
140
assert_image_similar(image, target, 25.0)
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")
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()