Skip to content

Commit c01ecac

Browse files
authored
Merge pull request #1 from radarhere/webp-params
Simplified code
2 parents 6904460 + 3c7aa13 commit c01ecac

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

Tests/test_file_webp_alpha.py

+20-25
Original file line numberDiff line numberDiff line change
@@ -97,38 +97,33 @@ def test_write_rgba(tmp_path):
9797
assert_image_similar(image, pil_image, 1.0)
9898

9999

100-
def test_write_rgba_keep_transparent(tmp_path):
100+
def test_keep_rgb_values_when_transparent(tmp_path):
101101
"""
102-
Can we write a RGBA mode file to WebP while preserving
103-
the transparent RGB without error.
104-
Does it have the bits we expect?
102+
Saving transparent pixels should retain their original RGB values
103+
when using the "exact" parameter.
105104
"""
106105

107-
temp_output_file = str(tmp_path / "temp.webp")
106+
image = hopper("RGB")
108107

109-
input_image = hopper("RGB")
110-
# make a copy of the image
111-
output_image = input_image.copy()
112-
# make a single channel image with the same size as input_image
113-
new_alpha = Image.new("L", input_image.size, 255)
114-
# make the left half transparent
115-
new_alpha.paste((0,), (0, 0, new_alpha.size[0] // 2, new_alpha.size[1]))
116-
# putalpha on output_image
117-
output_image.putalpha(new_alpha)
108+
# create a copy of the image
109+
# with the left half transparent
110+
half_transparent_image = image.copy()
111+
new_alpha = Image.new("L", (128, 128), 255)
112+
new_alpha.paste(0, (0, 0, 64, 128))
113+
half_transparent_image.putalpha(new_alpha)
118114

119-
# now save with transparent area preserved.
120-
output_image.save(temp_output_file, "WEBP", exact=True, lossless=True)
121-
# even though it is lossless, if we don't put exact=True, the transparent
122-
# area will be filled with black (or something more conducive to compression)
115+
# save with transparent area preserved
116+
temp_file = str(tmp_path / "temp.webp")
117+
half_transparent_image.save(temp_file, exact=True, lossless=True)
123118

124-
with Image.open(temp_output_file) as image:
125-
image.load()
119+
with Image.open(temp_file) as reloaded:
120+
assert reloaded.mode == "RGBA"
121+
assert reloaded.format == "WEBP"
126122

127-
assert image.mode == "RGBA"
128-
assert image.format == "WEBP"
129-
image.load()
130-
image = image.convert("RGB")
131-
assert_image_similar(image, input_image, 1.0)
123+
# even though it is lossless, if we don't use exact=True
124+
# in libwebp >= 0.5, the transparent area will be filled with black
125+
# (or something more conducive to compression)
126+
assert_image_equal(reloaded.convert("RGB"), image)
132127

133128

134129
def test_write_unsupported_mode_PA(tmp_path):

src/PIL/WebPImagePlugin.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def _save(im, fp, filename):
318318
exif = exif[6:]
319319
xmp = im.encoderinfo.get("xmp", "")
320320
method = im.encoderinfo.get("method", 4)
321-
exact = im.encoderinfo.get("exact", False)
321+
exact = 1 if im.encoderinfo.get("exact") else 0
322322

323323
if im.mode not in _VALID_WEBP_LEGACY_MODES:
324324
alpha = (
@@ -337,7 +337,7 @@ def _save(im, fp, filename):
337337
im.mode,
338338
icc_profile,
339339
method,
340-
1 if exact else 0,
340+
exact,
341341
exif,
342342
xmp,
343343
)

0 commit comments

Comments
 (0)