Merge branch 'webp-params' of https://github.com/ashafaei/Pillow into webp-params

This commit is contained in:
Alireza Shafaei 2022-11-18 23:51:09 -08:00
commit becdd31eec
2 changed files with 22 additions and 27 deletions

View File

@ -97,38 +97,33 @@ def test_write_rgba(tmp_path):
assert_image_similar(image, pil_image, 1.0) assert_image_similar(image, pil_image, 1.0)
def test_write_rgba_keep_transparent(tmp_path): def test_keep_rgb_values_when_transparent(tmp_path):
""" """
Can we write a RGBA mode file to WebP while preserving Saving transparent pixels should retain their original RGB values
the transparent RGB without error. when using the "exact" parameter.
Does it have the bits we expect?
""" """
temp_output_file = str(tmp_path / "temp.webp") image = hopper("RGB")
input_image = hopper("RGB") # create a copy of the image
# make a copy of the image # with the left half transparent
output_image = input_image.copy() half_transparent_image = image.copy()
# make a single channel image with the same size as input_image new_alpha = Image.new("L", (128, 128), 255)
new_alpha = Image.new("L", input_image.size, 255) new_alpha.paste(0, (0, 0, 64, 128))
# make the left half transparent half_transparent_image.putalpha(new_alpha)
new_alpha.paste((0,), (0, 0, new_alpha.size[0] // 2, new_alpha.size[1]))
# putalpha on output_image
output_image.putalpha(new_alpha)
# now save with transparent area preserved. # save with transparent area preserved
output_image.save(temp_output_file, "WEBP", exact=True, lossless=True) temp_file = str(tmp_path / "temp.webp")
# even though it is lossless, if we don't put exact=True, the transparent half_transparent_image.save(temp_file, exact=True, lossless=True)
# area will be filled with black (or something more conducive to compression)
with Image.open(temp_output_file) as image: with Image.open(temp_file) as reloaded:
image.load() assert reloaded.mode == "RGBA"
assert reloaded.format == "WEBP"
assert image.mode == "RGBA" # even though it is lossless, if we don't use exact=True
assert image.format == "WEBP" # in libwebp >= 0.5, the transparent area will be filled with black
image.load() # (or something more conducive to compression)
image = image.convert("RGB") assert_image_equal(reloaded.convert("RGB"), image)
assert_image_similar(image, input_image, 1.0)
def test_write_unsupported_mode_PA(tmp_path): def test_write_unsupported_mode_PA(tmp_path):

View File

@ -318,7 +318,7 @@ def _save(im, fp, filename):
exif = exif[6:] exif = exif[6:]
xmp = im.encoderinfo.get("xmp", "") xmp = im.encoderinfo.get("xmp", "")
method = im.encoderinfo.get("method", 4) method = im.encoderinfo.get("method", 4)
exact = im.encoderinfo.get("exact", False) exact = 1 if im.encoderinfo.get("exact") else 0
if im.mode not in _VALID_WEBP_LEGACY_MODES: if im.mode not in _VALID_WEBP_LEGACY_MODES:
alpha = ( alpha = (
@ -337,7 +337,7 @@ def _save(im, fp, filename):
im.mode, im.mode,
icc_profile, icc_profile,
method, method,
1 if exact else 0, exact,
exif, exif,
xmp, xmp,
) )