Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "exact" option when saving WebP #6747

Merged
merged 15 commits into from Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions Tests/test_file_webp_alpha.py
Expand Up @@ -97,6 +97,40 @@ def test_write_rgba(tmp_path):
assert_image_similar(image, pil_image, 1.0)


def test_write_rgba_keep_transparent(tmp_path):
"""
Can we write a RGBA mode file to WebP while preserving
the transparent RGB without error.
Does it have the bits we expect?
"""

temp_output_file = str(tmp_path / "temp.webp")

input_image = hopper("RGB")
# make a copy of the image
output_image = input_image.copy()
# make a single channel image with the same size as input_image
new_alpha = Image.new("L", input_image.size, 255)
# make the left half transparent
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.
output_image.save(temp_output_file, "WEBP", exact=True, lossless=True)
# even though it is lossless, if we don't put exact=True, the transparent
# area will be filled with black (or something more conducive to compression)

with Image.open(temp_output_file) as image:
image.load()

assert image.mode == "RGBA"
assert image.format == "WEBP"
image.load()
image = image.convert("RGB")
assert_image_similar(image, input_image, 1.0)


def test_write_unsupported_mode_PA(tmp_path):
"""
Saving a palette-based file with transparency to WebP format
Expand Down
5 changes: 5 additions & 0 deletions docs/handbook/image-file-formats.rst
Expand Up @@ -1124,6 +1124,11 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options:
**method**
Quality/speed trade-off (0=fast, 6=slower-better). Defaults to 4.

**exact**
hugovk marked this conversation as resolved.
Show resolved Hide resolved
If true, preserve the transparent RGB values. Otherwise, discard
invisible RGB values for better compression. Defaults to false.
Requires LibWebP 0.5.0 or later.
radarhere marked this conversation as resolved.
Show resolved Hide resolved

**icc_profile**
The ICC Profile to include in the saved file. Only supported if
the system WebP library was built with webpmux support.
Expand Down
2 changes: 2 additions & 0 deletions src/PIL/WebPImagePlugin.py
Expand Up @@ -318,6 +318,7 @@ def _save(im, fp, filename):
exif = exif[6:]
xmp = im.encoderinfo.get("xmp", "")
method = im.encoderinfo.get("method", 4)
exact = im.encoderinfo.get("exact", False)

if im.mode not in _VALID_WEBP_LEGACY_MODES:
alpha = (
Expand All @@ -336,6 +337,7 @@ def _save(im, fp, filename):
im.mode,
icc_profile,
method,
1 if exact else 0,
exif,
xmp,
)
Expand Down
8 changes: 7 additions & 1 deletion src/_webp.c
Expand Up @@ -576,6 +576,7 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) {
int lossless;
float quality_factor;
int method;
int exact;
uint8_t *rgb;
uint8_t *icc_bytes;
uint8_t *exif_bytes;
Expand All @@ -597,7 +598,7 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) {

if (!PyArg_ParseTuple(
args,
"y#iiifss#is#s#",
"y#iiifss#iis#s#",
(char **)&rgb,
&size,
&width,
Expand All @@ -608,6 +609,7 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) {
&icc_bytes,
&icc_size,
&method,
&exact,
&exif_bytes,
&exif_size,
&xmp_bytes,
Expand All @@ -633,6 +635,10 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) {
config.lossless = lossless;
config.quality = quality_factor;
config.method = method;
#if WEBP_ENCODER_ABI_VERSION >= 0x0209
// the exact flag is only available in libwebp 0.5.0 and later
ashafaei marked this conversation as resolved.
Show resolved Hide resolved
config.exact = exact;
#endif

// Validate the config
if (!WebPValidateConfig(&config)) {
Expand Down