From c1e8375af834b190386cd2cfa30703c7975675a8 Mon Sep 17 00:00:00 2001 From: Aleksandr Karpinskii Date: Sun, 7 Jul 2024 21:51:14 +0400 Subject: [PATCH 01/14] Require webpmux and webpdemux --- Tests/check_wheel.py | 1 - Tests/test_features.py | 5 --- Tests/test_file_webp_metadata.py | 5 +-- docs/handbook/image-file-formats.rst | 9 ++--- docs/installation/building-from-source.rst | 8 ++-- docs/reference/features.rst | 1 - setup.py | 46 ++++++++-------------- src/PIL/features.py | 2 - src/_webp.c | 33 ---------------- winbuild/build_prepare.py | 2 +- 10 files changed, 26 insertions(+), 86 deletions(-) diff --git a/Tests/check_wheel.py b/Tests/check_wheel.py index 4b91984f58a..2e4498db6c3 100644 --- a/Tests/check_wheel.py +++ b/Tests/check_wheel.py @@ -28,7 +28,6 @@ def test_wheel_codecs() -> None: def test_wheel_features() -> None: expected_features = { "webp_anim", - "webp_mux", "transp_webp", "raqm", "fribidi", diff --git a/Tests/test_features.py b/Tests/test_features.py index b7eefa09ae1..33509a346af 100644 --- a/Tests/test_features.py +++ b/Tests/test_features.py @@ -57,11 +57,6 @@ def test_webp_transparency() -> None: assert features.check("transp_webp") == _webp.HAVE_TRANSPARENCY -@skip_unless_feature("webp") -def test_webp_mux() -> None: - assert features.check("webp_mux") == _webp.HAVE_WEBPMUX - - @skip_unless_feature("webp") def test_webp_anim() -> None: assert features.check("webp_anim") == _webp.HAVE_WEBPANIM diff --git a/Tests/test_file_webp_metadata.py b/Tests/test_file_webp_metadata.py index c3df4ad7bcb..52a84a83e9b 100644 --- a/Tests/test_file_webp_metadata.py +++ b/Tests/test_file_webp_metadata.py @@ -10,10 +10,7 @@ from .helper import mark_if_feature_version, skip_unless_feature -pytestmark = [ - skip_unless_feature("webp"), - skip_unless_feature("webp_mux"), -] +pytestmark = [skip_unless_feature("webp")] ElementTree: ModuleType | None try: diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index 1ec97214900..8561cc4aa29 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -1252,16 +1252,13 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options: Requires libwebp 0.5.0 or later. **icc_profile** - The ICC Profile to include in the saved file. Only supported if - the system WebP library was built with webpmux support. + The ICC Profile to include in the saved file. **exif** - The exif data to include in the saved file. Only supported if - the system WebP library was built with webpmux support. + The exif data to include in the saved file. **xmp** - The XMP data to include in the saved file. Only supported if - the system WebP library was built with webpmux support. + The XMP data to include in the saved file. Saving sequences ~~~~~~~~~~~~~~~~ diff --git a/docs/installation/building-from-source.rst b/docs/installation/building-from-source.rst index 7f7dfa6ff24..f47edbc5bcc 100644 --- a/docs/installation/building-from-source.rst +++ b/docs/installation/building-from-source.rst @@ -275,18 +275,18 @@ Build Options * Config settings: ``-C zlib=disable``, ``-C jpeg=disable``, ``-C tiff=disable``, ``-C freetype=disable``, ``-C raqm=disable``, - ``-C lcms=disable``, ``-C webp=disable``, ``-C webpmux=disable``, + ``-C lcms=disable``, ``-C webp=disable``, ``-C jpeg2000=disable``, ``-C imagequant=disable``, ``-C xcb=disable``. Disable building the corresponding feature even if the development libraries are present on the building machine. * Config settings: ``-C zlib=enable``, ``-C jpeg=enable``, ``-C tiff=enable``, ``-C freetype=enable``, ``-C raqm=enable``, - ``-C lcms=enable``, ``-C webp=enable``, ``-C webpmux=enable``, + ``-C lcms=enable``, ``-C webp=enable``, ``-C jpeg2000=enable``, ``-C imagequant=enable``, ``-C xcb=enable``. Require that the corresponding feature is built. The build will raise - an exception if the libraries are not found. Webpmux (WebP metadata) - relies on WebP support. Tcl and Tk also must be used together. + an exception if the libraries are not found. Tcl and Tk must be used + together. * Config settings: ``-C raqm=vendor``, ``-C fribidi=vendor``. These flags are used to compile a modified version of libraqm and diff --git a/docs/reference/features.rst b/docs/reference/features.rst index c6619306186..55c0b120007 100644 --- a/docs/reference/features.rst +++ b/docs/reference/features.rst @@ -55,7 +55,6 @@ Support for the following features can be checked: * ``libjpeg_turbo``: (compile time) Whether Pillow was compiled against the libjpeg-turbo version of libjpeg. Compile-time version number is available. * ``transp_webp``: Support for transparency in WebP images. -* ``webp_mux``: (compile time) Support for EXIF data in WebP images. * ``webp_anim``: (compile time) Support for animated WebP images. * ``raqm``: Raqm library, required for ``ImageFont.Layout.RAQM`` in :py:func:`PIL.ImageFont.truetype`. Run-time version number is available for Raqm 0.7.0 or newer. * ``libimagequant``: (compile time) ImageQuant quantization support in :py:func:`PIL.Image.Image.quantize`. Run-time version number is available. diff --git a/setup.py b/setup.py index b26852b0b07..8d0dfb6fa21 100644 --- a/setup.py +++ b/setup.py @@ -295,7 +295,6 @@ class feature: "raqm", "lcms", "webp", - "webpmux", "jpeg2000", "imagequant", "xcb", @@ -794,28 +793,21 @@ def build_extensions(self): if feature.want("webp"): _dbg("Looking for webp") - if _find_include_file(self, "webp/encode.h") and _find_include_file( - self, "webp/decode.h" + if all( + _find_include_file(self, src) + for src in ["webp/encode.h", "webp/mux.h", "webp/demux.h"] ): # In Google's precompiled zip it is call "libwebp": - if _find_library_file(self, "webp"): - feature.webp = "webp" - elif _find_library_file(self, "libwebp"): - feature.webp = "libwebp" - - if feature.want("webpmux"): - _dbg("Looking for webpmux") - if _find_include_file(self, "webp/mux.h") and _find_include_file( - self, "webp/demux.h" - ): - if _find_library_file(self, "webpmux") and _find_library_file( - self, "webpdemux" + if all( + _find_library_file(self, lib) + for lib in ["webp", "webpmux", "webpdemux"] ): - feature.webpmux = "webpmux" - if _find_library_file(self, "libwebpmux") and _find_library_file( - self, "libwebpdemux" + feature.webp = "webp" + elif all( + _find_library_file(self, lib) + for lib in ["libwebp", "libwebpmux", "libwebpdemux"] ): - feature.webpmux = "libwebpmux" + feature.webp = "libwebp" if feature.want("xcb"): _dbg("Looking for xcb") @@ -904,15 +896,12 @@ def build_extensions(self): self._remove_extension("PIL._imagingcms") if feature.webp: - libs = [feature.webp] - defs = [] - - if feature.webpmux: - defs.append(("HAVE_WEBPMUX", None)) - libs.append(feature.webpmux) - libs.append(feature.webpmux.replace("pmux", "pdemux")) - - self._update_extension("PIL._webp", libs, defs) + libs = [ + feature.webp, + feature.webp + "mux", + feature.webp + "demux", + ] + self._update_extension("PIL._webp", libs, []) else: self._remove_extension("PIL._webp") @@ -953,7 +942,6 @@ def summary_report(self, feature): (feature.raqm, "RAQM (Text shaping)", raqm_extra_info), (feature.lcms, "LITTLECMS2"), (feature.webp, "WEBP"), - (feature.webpmux, "WEBPMUX"), (feature.xcb, "XCB (X protocol)"), ] diff --git a/src/PIL/features.py b/src/PIL/features.py index 13908c4eb78..7f6cdb1618f 100644 --- a/src/PIL/features.py +++ b/src/PIL/features.py @@ -120,7 +120,6 @@ def get_supported_codecs() -> list[str]: features = { "webp_anim": ("PIL._webp", "HAVE_WEBPANIM", None), - "webp_mux": ("PIL._webp", "HAVE_WEBPMUX", None), "transp_webp": ("PIL._webp", "HAVE_TRANSPARENCY", None), "raqm": ("PIL._imagingft", "HAVE_RAQM", "raqm_version"), "fribidi": ("PIL._imagingft", "HAVE_FRIBIDI", "fribidi_version"), @@ -272,7 +271,6 @@ def pilinfo(out: IO[str] | None = None, supported_formats: bool = True) -> None: ("littlecms2", "LITTLECMS2"), ("webp", "WEBP"), ("transp_webp", "WEBP Transparency"), - ("webp_mux", "WEBPMUX"), ("webp_anim", "WEBP Animation"), ("jpg", "JPEG"), ("jpg_2000", "OPENJPEG (JPEG2000)"), diff --git a/src/_webp.c b/src/_webp.c index e686ec820fd..ab229058b4d 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -4,8 +4,6 @@ #include #include #include - -#ifdef HAVE_WEBPMUX #include #include @@ -19,8 +17,6 @@ #define HAVE_WEBPANIM #endif -#endif - void ImagingSectionEnter(ImagingSectionCookie *cookie) { *cookie = (PyThreadState *)PyEval_SaveThread(); @@ -35,8 +31,6 @@ ImagingSectionLeave(ImagingSectionCookie *cookie) { /* WebP Muxer Error Handling */ /* -------------------------------------------------------------------- */ -#ifdef HAVE_WEBPMUX - static const char *const kErrorMessages[-WEBP_MUX_NOT_ENOUGH_DATA + 1] = { "WEBP_MUX_NOT_FOUND", "WEBP_MUX_INVALID_ARGUMENT", @@ -89,8 +83,6 @@ HandleMuxError(WebPMuxError err, char *chunk) { return NULL; } -#endif - /* -------------------------------------------------------------------- */ /* WebP Animation Support */ /* -------------------------------------------------------------------- */ @@ -693,13 +685,6 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) { output = writer.mem; ret_size = writer.size; -#ifndef HAVE_WEBPMUX - if (ret_size > 0) { - PyObject *ret = PyBytes_FromStringAndSize((char *)output, ret_size); - free(output); - return ret; - } -#else { /* I want to truncate the *_size items that get passed into WebP data. Pypy2.1.0 had some issues where the Py_ssize_t items had @@ -775,7 +760,6 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) { return ret; } } -#endif Py_RETURN_NONE; } @@ -809,9 +793,6 @@ WebPDecode_wrapper(PyObject *self, PyObject *args) { mode = "RGBA"; } -#ifndef HAVE_WEBPMUX - vp8_status_code = WebPDecode(webp, size, &config); -#else { int copy_data = 0; WebPData data = {webp, size}; @@ -849,7 +830,6 @@ WebPDecode_wrapper(PyObject *self, PyObject *args) { WebPDataClear(&image.bitstream); WebPMuxDelete(mux); } -#endif } if (vp8_status_code != VP8_STATUS_OK) { @@ -949,18 +929,6 @@ static PyMethodDef webpMethods[] = { {NULL, NULL} }; -void -addMuxFlagToModule(PyObject *m) { - PyObject *have_webpmux; -#ifdef HAVE_WEBPMUX - have_webpmux = Py_True; -#else - have_webpmux = Py_False; -#endif - Py_INCREF(have_webpmux); - PyModule_AddObject(m, "HAVE_WEBPMUX", have_webpmux); -} - void addAnimFlagToModule(PyObject *m) { PyObject *have_webpanim; @@ -991,7 +959,6 @@ setup_module(PyObject *m) { } #endif PyObject *d = PyModule_GetDict(m); - addMuxFlagToModule(m); addAnimFlagToModule(m); addTransparencyFlagToModule(m); diff --git a/winbuild/build_prepare.py b/winbuild/build_prepare.py index 9837589b2b1..7129699eb52 100644 --- a/winbuild/build_prepare.py +++ b/winbuild/build_prepare.py @@ -201,7 +201,7 @@ def cmd_msbuild( }, "build": [ *cmds_cmake( - "webp webpdemux webpmux", + "webp webpmux webpdemux", "-DBUILD_SHARED_LIBS:BOOL=OFF", "-DWEBP_LINK_STATIC:BOOL=OFF", ), From 9bed5b426437ad4aacd36288fcfb9422cac8c9c9 Mon Sep 17 00:00:00 2001 From: Aleksandr Karpinskii Date: Sun, 7 Jul 2024 22:05:57 +0400 Subject: [PATCH 02/14] Remove _webp.WebPDecoderBuggyAlpha and _webp.HAVE_TRANSPARENCY --- Tests/check_wheel.py | 1 - Tests/test_features.py | 6 ------ Tests/test_file_webp.py | 1 - Tests/test_file_webp_alpha.py | 8 -------- docs/reference/features.rst | 1 - src/PIL/features.py | 2 -- src/_webp.c | 27 --------------------------- 7 files changed, 46 deletions(-) diff --git a/Tests/check_wheel.py b/Tests/check_wheel.py index 2e4498db6c3..f862e4fff89 100644 --- a/Tests/check_wheel.py +++ b/Tests/check_wheel.py @@ -28,7 +28,6 @@ def test_wheel_codecs() -> None: def test_wheel_features() -> None: expected_features = { "webp_anim", - "transp_webp", "raqm", "fribidi", "harfbuzz", diff --git a/Tests/test_features.py b/Tests/test_features.py index 33509a346af..577e7a5d5c8 100644 --- a/Tests/test_features.py +++ b/Tests/test_features.py @@ -51,12 +51,6 @@ def test(name: str, function: Callable[[str], str | None]) -> None: test(feature, features.version_feature) -@skip_unless_feature("webp") -def test_webp_transparency() -> None: - assert features.check("transp_webp") != _webp.WebPDecoderBuggyAlpha() - assert features.check("transp_webp") == _webp.HAVE_TRANSPARENCY - - @skip_unless_feature("webp") def test_webp_anim() -> None: assert features.check("webp_anim") == _webp.HAVE_WEBPANIM diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index cbc905de4e7..f132d0a64b4 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -49,7 +49,6 @@ def setup_method(self) -> None: def test_version(self) -> None: _webp.WebPDecoderVersion() - _webp.WebPDecoderBuggyAlpha() version = features.version_module("webp") assert version is not None assert re.search(r"\d+\.\d+\.\d+$", version) diff --git a/Tests/test_file_webp_alpha.py b/Tests/test_file_webp_alpha.py index c7445212110..e80ef7d4f5b 100644 --- a/Tests/test_file_webp_alpha.py +++ b/Tests/test_file_webp_alpha.py @@ -16,11 +16,6 @@ _webp = pytest.importorskip("PIL._webp", reason="WebP support not installed") -def setup_module() -> None: - if _webp.WebPDecoderBuggyAlpha(): - pytest.skip("Buggy early version of WebP installed, not testing transparency") - - def test_read_rgba() -> None: """ Can we read an RGBA mode file without error? @@ -81,9 +76,6 @@ def test_write_rgba(tmp_path: Path) -> None: 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() diff --git a/docs/reference/features.rst b/docs/reference/features.rst index 55c0b120007..d4fb340bdc6 100644 --- a/docs/reference/features.rst +++ b/docs/reference/features.rst @@ -54,7 +54,6 @@ Feature version numbers are available only where stated. Support for the following features can be checked: * ``libjpeg_turbo``: (compile time) Whether Pillow was compiled against the libjpeg-turbo version of libjpeg. Compile-time version number is available. -* ``transp_webp``: Support for transparency in WebP images. * ``webp_anim``: (compile time) Support for animated WebP images. * ``raqm``: Raqm library, required for ``ImageFont.Layout.RAQM`` in :py:func:`PIL.ImageFont.truetype`. Run-time version number is available for Raqm 0.7.0 or newer. * ``libimagequant``: (compile time) ImageQuant quantization support in :py:func:`PIL.Image.Image.quantize`. Run-time version number is available. diff --git a/src/PIL/features.py b/src/PIL/features.py index 7f6cdb1618f..60590dc098d 100644 --- a/src/PIL/features.py +++ b/src/PIL/features.py @@ -120,7 +120,6 @@ def get_supported_codecs() -> list[str]: features = { "webp_anim": ("PIL._webp", "HAVE_WEBPANIM", None), - "transp_webp": ("PIL._webp", "HAVE_TRANSPARENCY", None), "raqm": ("PIL._imagingft", "HAVE_RAQM", "raqm_version"), "fribidi": ("PIL._imagingft", "HAVE_FRIBIDI", "fribidi_version"), "harfbuzz": ("PIL._imagingft", "HAVE_HARFBUZZ", "harfbuzz_version"), @@ -270,7 +269,6 @@ def pilinfo(out: IO[str] | None = None, supported_formats: bool = True) -> None: ("freetype2", "FREETYPE2"), ("littlecms2", "LITTLECMS2"), ("webp", "WEBP"), - ("transp_webp", "WEBP Transparency"), ("webp_anim", "WEBP Animation"), ("jpg", "JPEG"), ("jpg_2000", "OPENJPEG (JPEG2000)"), diff --git a/src/_webp.c b/src/_webp.c index ab229058b4d..a10eb111c3b 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -896,20 +896,6 @@ WebPDecoderVersion_str(void) { return version; } -/* - * The version of webp that ships with (0.1.3) Ubuntu 12.04 doesn't handle alpha well. - * Files that are valid with 0.3 are reported as being invalid. - */ -int -WebPDecoderBuggyAlpha(void) { - return WebPGetDecoderVersion() == 0x0103; -} - -PyObject * -WebPDecoderBuggyAlpha_wrapper() { - return Py_BuildValue("i", WebPDecoderBuggyAlpha()); -} - /* -------------------------------------------------------------------- */ /* Module Setup */ /* -------------------------------------------------------------------- */ @@ -922,10 +908,6 @@ static PyMethodDef webpMethods[] = { {"WebPEncode", WebPEncode_wrapper, METH_VARARGS, "WebPEncode"}, {"WebPDecode", WebPDecode_wrapper, METH_VARARGS, "WebPDecode"}, {"WebPDecoderVersion", WebPDecoderVersion_wrapper, METH_NOARGS, "WebPVersion"}, - {"WebPDecoderBuggyAlpha", - WebPDecoderBuggyAlpha_wrapper, - METH_NOARGS, - "WebPDecoderBuggyAlpha"}, {NULL, NULL} }; @@ -941,14 +923,6 @@ addAnimFlagToModule(PyObject *m) { PyModule_AddObject(m, "HAVE_WEBPANIM", have_webpanim); } -void -addTransparencyFlagToModule(PyObject *m) { - PyObject *have_transparency = PyBool_FromLong(!WebPDecoderBuggyAlpha()); - if (PyModule_AddObject(m, "HAVE_TRANSPARENCY", have_transparency)) { - Py_DECREF(have_transparency); - } -} - static int setup_module(PyObject *m) { #ifdef HAVE_WEBPANIM @@ -960,7 +934,6 @@ setup_module(PyObject *m) { #endif PyObject *d = PyModule_GetDict(m); addAnimFlagToModule(m); - addTransparencyFlagToModule(m); PyObject *v = PyUnicode_FromString(WebPDecoderVersion_str()); PyDict_SetItemString(d, "webpdecoder_version", v ? v : Py_None); From a3468996c0b7b6df2b685ff21c4f515f5105ff8c Mon Sep 17 00:00:00 2001 From: Aleksandr Karpinskii Date: Sun, 7 Jul 2024 22:47:06 +0400 Subject: [PATCH 03/14] Remove webp animations flags and conditions Removed: _webp.WebPDecode _webp.HAVE_WEBPANIM features.webp_anim --- Tests/check_wheel.py | 1 - Tests/test_features.py | 10 -- Tests/test_file_gif.py | 2 +- Tests/test_file_webp.py | 24 ++--- Tests/test_file_webp_alpha.py | 7 +- Tests/test_file_webp_animated.py | 5 +- Tests/test_file_webp_lossless.py | 3 - Tests/test_file_webp_metadata.py | 3 +- Tests/test_image.py | 1 - Tests/test_imagefile.py | 1 - Tests/test_imageops.py | 2 +- docs/handbook/image-file-formats.rst | 9 +- docs/reference/features.rst | 1 - setup.py | 7 +- src/PIL/WebPImagePlugin.py | 51 +++------ src/PIL/features.py | 2 - src/_webp.c | 150 +-------------------------- 17 files changed, 36 insertions(+), 243 deletions(-) diff --git a/Tests/check_wheel.py b/Tests/check_wheel.py index f862e4fff89..8fcb75dad5c 100644 --- a/Tests/check_wheel.py +++ b/Tests/check_wheel.py @@ -27,7 +27,6 @@ def test_wheel_codecs() -> None: def test_wheel_features() -> None: expected_features = { - "webp_anim", "raqm", "fribidi", "harfbuzz", diff --git a/Tests/test_features.py b/Tests/test_features.py index 577e7a5d5c8..40466082f7a 100644 --- a/Tests/test_features.py +++ b/Tests/test_features.py @@ -10,11 +10,6 @@ from .helper import skip_unless_feature -try: - from PIL import _webp -except ImportError: - pass - def test_check() -> None: # Check the correctness of the convenience function @@ -51,11 +46,6 @@ def test(name: str, function: Callable[[str], str | None]) -> None: test(feature, features.version_feature) -@skip_unless_feature("webp") -def test_webp_anim() -> None: - assert features.check("webp_anim") == _webp.HAVE_WEBPANIM - - @skip_unless_feature("libjpeg_turbo") def test_libjpeg_turbo_version() -> None: version = features.version("libjpeg_turbo") diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 85b017d29af..8cefdb628e6 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -978,7 +978,7 @@ def test_webp_background(tmp_path: Path) -> None: out = str(tmp_path / "temp.gif") # Test opaque WebP background - if features.check("webp") and features.check("webp_anim"): + if features.check("webp"): with Image.open("Tests/images/hopper.webp") as im: assert im.info["background"] == (255, 255, 255, 255) im.save(out) diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index f132d0a64b4..6e9eea4aec0 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -48,7 +48,6 @@ def setup_method(self) -> None: self.rgb_mode = "RGB" def test_version(self) -> None: - _webp.WebPDecoderVersion() version = features.version_module("webp") assert version is not None assert re.search(r"\d+\.\d+\.\d+$", version) @@ -116,7 +115,6 @@ def test_write_method(self, tmp_path: Path) -> None: hopper().save(buffer_method, format="WEBP", method=6) assert buffer_no_args.getbuffer() != buffer_method.getbuffer() - @skip_unless_feature("webp_anim") def test_save_all(self, tmp_path: Path) -> None: temp_file = str(tmp_path / "temp.webp") im = Image.new("RGB", (1, 1)) @@ -131,10 +129,9 @@ def test_save_all(self, tmp_path: Path) -> None: def test_icc_profile(self, tmp_path: Path) -> None: self._roundtrip(tmp_path, self.rgb_mode, 12.5, {"icc_profile": None}) - if _webp.HAVE_WEBPANIM: - self._roundtrip( - tmp_path, self.rgb_mode, 12.5, {"icc_profile": None, "save_all": True} - ) + self._roundtrip( + tmp_path, self.rgb_mode, 12.5, {"icc_profile": None, "save_all": True} + ) def test_write_unsupported_mode_L(self, tmp_path: Path) -> None: """ @@ -164,10 +161,8 @@ def test_WebPEncode_with_invalid_args(self) -> None: """ Calling encoder functions with no arguments should result in an error. """ - - if _webp.HAVE_WEBPANIM: - with pytest.raises(TypeError): - _webp.WebPAnimEncoder() + with pytest.raises(TypeError): + _webp.WebPAnimEncoder() with pytest.raises(TypeError): _webp.WebPEncode() @@ -175,12 +170,8 @@ def test_WebPDecode_with_invalid_args(self) -> None: """ Calling decoder functions with no arguments should result in an error. """ - - if _webp.HAVE_WEBPANIM: - with pytest.raises(TypeError): - _webp.WebPAnimDecoder() with pytest.raises(TypeError): - _webp.WebPDecode() + _webp.WebPAnimDecoder() def test_no_resource_warning(self, tmp_path: Path) -> None: file_path = "Tests/images/hopper.webp" @@ -199,7 +190,6 @@ def test_file_pointer_could_be_reused(self) -> None: "background", (0, (0,), (-1, 0, 1, 2), (253, 254, 255, 256)), ) - @skip_unless_feature("webp_anim") def test_invalid_background( self, background: int | tuple[int, ...], tmp_path: Path ) -> None: @@ -208,7 +198,6 @@ def test_invalid_background( with pytest.raises(OSError): im.save(temp_file, save_all=True, append_images=[im], background=background) - @skip_unless_feature("webp_anim") def test_background_from_gif(self, tmp_path: Path) -> None: # Save L mode GIF with background with Image.open("Tests/images/no_palette_with_background.gif") as im: @@ -233,7 +222,6 @@ def test_background_from_gif(self, tmp_path: Path) -> None: difference = sum(abs(original_value[i] - reread_value[i]) for i in range(0, 3)) assert difference < 5 - @skip_unless_feature("webp_anim") def test_duration(self, tmp_path: Path) -> None: with Image.open("Tests/images/dispose_bgnd.gif") as im: assert im.info["duration"] == 1000 diff --git a/Tests/test_file_webp_alpha.py b/Tests/test_file_webp_alpha.py index e80ef7d4f5b..53cdfff0327 100644 --- a/Tests/test_file_webp_alpha.py +++ b/Tests/test_file_webp_alpha.py @@ -85,12 +85,7 @@ def test_write_rgba(tmp_path: Path) -> None: 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) + assert_image_similar(image, pil_image, 1.0) def test_keep_rgb_values_when_transparent(tmp_path: Path) -> None: diff --git a/Tests/test_file_webp_animated.py b/Tests/test_file_webp_animated.py index e0d7999e323..967a0aae847 100644 --- a/Tests/test_file_webp_animated.py +++ b/Tests/test_file_webp_animated.py @@ -15,10 +15,7 @@ skip_unless_feature, ) -pytestmark = [ - skip_unless_feature("webp"), - skip_unless_feature("webp_anim"), -] +pytestmark = skip_unless_feature("webp") def test_n_frames() -> None: diff --git a/Tests/test_file_webp_lossless.py b/Tests/test_file_webp_lossless.py index 32e29de56ad..dc98ad3f013 100644 --- a/Tests/test_file_webp_lossless.py +++ b/Tests/test_file_webp_lossless.py @@ -13,9 +13,6 @@ def test_write_lossless_rgb(tmp_path: Path) -> None: - if _webp.WebPDecoderVersion() < 0x0200: - pytest.skip("lossless not included") - temp_file = str(tmp_path / "temp.webp") hopper(RGB_MODE).save(temp_file, lossless=True) diff --git a/Tests/test_file_webp_metadata.py b/Tests/test_file_webp_metadata.py index 52a84a83e9b..4ef3d95f259 100644 --- a/Tests/test_file_webp_metadata.py +++ b/Tests/test_file_webp_metadata.py @@ -10,7 +10,7 @@ from .helper import mark_if_feature_version, skip_unless_feature -pytestmark = [skip_unless_feature("webp")] +pytestmark = skip_unless_feature("webp") ElementTree: ModuleType | None try: @@ -133,7 +133,6 @@ def test_getxmp() -> None: ) -@skip_unless_feature("webp_anim") def test_write_animated_metadata(tmp_path: Path) -> None: iccp_data = b"" exif_data = b"" diff --git a/Tests/test_image.py b/Tests/test_image.py index d8372789be9..719732d127b 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -817,7 +817,6 @@ def test_exif_jpeg(self, tmp_path: Path) -> None: assert reloaded_exif[305] == "Pillow test" @skip_unless_feature("webp") - @skip_unless_feature("webp_anim") def test_exif_webp(self, tmp_path: Path) -> None: with Image.open("Tests/images/hopper.webp") as im: exif = im.getexif() diff --git a/Tests/test_imagefile.py b/Tests/test_imagefile.py index 44a6e6a42f1..fe7d44785ce 100644 --- a/Tests/test_imagefile.py +++ b/Tests/test_imagefile.py @@ -94,7 +94,6 @@ def test_ico(self) -> None: assert (48, 48) == p.image.size @skip_unless_feature("webp") - @skip_unless_feature("webp_anim") def test_incremental_webp(self) -> None: with ImageFile.Parser() as p: with open("Tests/images/hopper.webp", "rb") as f: diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index e33e6d4c842..2fb2a60b632 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -390,7 +390,7 @@ def test_colorize_3color_offset() -> None: def test_exif_transpose() -> None: exts = [".jpg"] - if features.check("webp") and features.check("webp_anim"): + if features.check("webp"): exts.append(".webp") for ext in exts: with Image.open("Tests/images/hopper" + ext) as base_im: diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index 8561cc4aa29..49a17bb5c4f 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -1220,8 +1220,7 @@ using the general tags available through tiffinfo. WebP ^^^^ -Pillow reads and writes WebP files. The specifics of Pillow's capabilities with -this format are currently undocumented. +Pillow reads and writes WebP files. Requires libwebp v0.5.0 or later. .. _webp-saving: @@ -1263,12 +1262,6 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options: Saving sequences ~~~~~~~~~~~~~~~~ -.. note:: - - Support for animated WebP files will only be enabled if the system WebP - library is v0.5.0 or later. You can check webp animation support at - runtime by calling ``features.check("webp_anim")``. - When calling :py:meth:`~PIL.Image.Image.save` to write a WebP file, by default only the first frame of a multiframe image will be saved. If the ``save_all`` argument is present and true, then all frames will be saved, and the following diff --git a/docs/reference/features.rst b/docs/reference/features.rst index d4fb340bdc6..26c8ab8cd69 100644 --- a/docs/reference/features.rst +++ b/docs/reference/features.rst @@ -54,7 +54,6 @@ Feature version numbers are available only where stated. Support for the following features can be checked: * ``libjpeg_turbo``: (compile time) Whether Pillow was compiled against the libjpeg-turbo version of libjpeg. Compile-time version number is available. -* ``webp_anim``: (compile time) Support for animated WebP images. * ``raqm``: Raqm library, required for ``ImageFont.Layout.RAQM`` in :py:func:`PIL.ImageFont.truetype`. Run-time version number is available for Raqm 0.7.0 or newer. * ``libimagequant``: (compile time) ImageQuant quantization support in :py:func:`PIL.Image.Image.quantize`. Run-time version number is available. * ``xcb``: (compile time) Support for X11 in :py:func:`PIL.ImageGrab.grab` via the XCB library. diff --git a/setup.py b/setup.py index 8d0dfb6fa21..edfa9ee28fc 100644 --- a/setup.py +++ b/setup.py @@ -795,7 +795,12 @@ def build_extensions(self): _dbg("Looking for webp") if all( _find_include_file(self, src) - for src in ["webp/encode.h", "webp/mux.h", "webp/demux.h"] + for src in [ + "webp/encode.h", + "webp/decode.h", + "webp/mux.h", + "webp/demux.h", + ] ): # In Google's precompiled zip it is call "libwebp": if all( diff --git a/src/PIL/WebPImagePlugin.py b/src/PIL/WebPImagePlugin.py index cec7963405b..fc8f4fc2edc 100644 --- a/src/PIL/WebPImagePlugin.py +++ b/src/PIL/WebPImagePlugin.py @@ -45,22 +45,6 @@ class WebPImageFile(ImageFile.ImageFile): __logical_frame = 0 def _open(self) -> None: - if not _webp.HAVE_WEBPANIM: - # Legacy mode - data, width, height, self._mode, icc_profile, exif = _webp.WebPDecode( - self.fp.read() - ) - if icc_profile: - self.info["icc_profile"] = icc_profile - if exif: - self.info["exif"] = exif - self._size = width, height - self.fp = BytesIO(data) - self.tile = [("raw", (0, 0) + self.size, 0, self.mode)] - self.n_frames = 1 - self.is_animated = False - return - # Use the newer AnimDecoder API to parse the (possibly) animated file, # and access muxed chunks like ICC/EXIF/XMP. self._decoder = _webp.WebPAnimDecoder(self.fp.read()) @@ -145,21 +129,20 @@ def _seek(self, frame: int) -> None: self._get_next() # Advance to the requested frame def load(self) -> Image.core.PixelAccess | None: - if _webp.HAVE_WEBPANIM: - if self.__loaded != self.__logical_frame: - self._seek(self.__logical_frame) - - # We need to load the image data for this frame - data, timestamp, duration = self._get_next() - self.info["timestamp"] = timestamp - self.info["duration"] = duration - self.__loaded = self.__logical_frame - - # Set tile - if self.fp and self._exclusive_fp: - self.fp.close() - self.fp = BytesIO(data) - self.tile = [("raw", (0, 0) + self.size, 0, self.rawmode)] + if self.__loaded != self.__logical_frame: + self._seek(self.__logical_frame) + + # We need to load the image data for this frame + data, timestamp, duration = self._get_next() + self.info["timestamp"] = timestamp + self.info["duration"] = duration + self.__loaded = self.__logical_frame + + # Set tile + if self.fp and self._exclusive_fp: + self.fp.close() + self.fp = BytesIO(data) + self.tile = [("raw", (0, 0) + self.size, 0, self.rawmode)] return super().load() @@ -167,9 +150,6 @@ def load_seek(self, pos: int) -> None: pass def tell(self) -> int: - if not _webp.HAVE_WEBPANIM: - return super().tell() - return self.__logical_frame @@ -357,7 +337,6 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: Image.register_open(WebPImageFile.format, WebPImageFile, _accept) if SUPPORTED: Image.register_save(WebPImageFile.format, _save) - if _webp.HAVE_WEBPANIM: - Image.register_save_all(WebPImageFile.format, _save_all) + Image.register_save_all(WebPImageFile.format, _save_all) Image.register_extension(WebPImageFile.format, ".webp") Image.register_mime(WebPImageFile.format, "image/webp") diff --git a/src/PIL/features.py b/src/PIL/features.py index 60590dc098d..bbf7c641b43 100644 --- a/src/PIL/features.py +++ b/src/PIL/features.py @@ -119,7 +119,6 @@ def get_supported_codecs() -> list[str]: features = { - "webp_anim": ("PIL._webp", "HAVE_WEBPANIM", None), "raqm": ("PIL._imagingft", "HAVE_RAQM", "raqm_version"), "fribidi": ("PIL._imagingft", "HAVE_FRIBIDI", "fribidi_version"), "harfbuzz": ("PIL._imagingft", "HAVE_HARFBUZZ", "harfbuzz_version"), @@ -269,7 +268,6 @@ def pilinfo(out: IO[str] | None = None, supported_formats: bool = True) -> None: ("freetype2", "FREETYPE2"), ("littlecms2", "LITTLECMS2"), ("webp", "WEBP"), - ("webp_anim", "WEBP Animation"), ("jpg", "JPEG"), ("jpg_2000", "OPENJPEG (JPEG2000)"), ("zlib", "ZLIB (PNG/ZIP)"), diff --git a/src/_webp.c b/src/_webp.c index a10eb111c3b..8302925b0fc 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -13,8 +13,8 @@ * very early versions had some significant differences, so we require later * versions, before enabling animation support. */ -#if WEBP_MUX_ABI_VERSION >= 0x0104 && WEBP_DEMUX_ABI_VERSION >= 0x0105 -#define HAVE_WEBPANIM +#if WEBP_MUX_ABI_VERSION < 0x0106 || WEBP_DEMUX_ABI_VERSION < 0x0107 +#error libwebp 0.5.0 and above is required. Upgrade libwebp or build with --disable-webp flag #endif void @@ -87,8 +87,6 @@ HandleMuxError(WebPMuxError err, char *chunk) { /* WebP Animation Support */ /* -------------------------------------------------------------------- */ -#ifdef HAVE_WEBPANIM - // Encoder type typedef struct { PyObject_HEAD WebPAnimEncoder *enc; @@ -568,8 +566,6 @@ static PyTypeObject WebPAnimDecoder_Type = { 0, /*tp_getset*/ }; -#endif - /* -------------------------------------------------------------------- */ /* Legacy WebP Support */ /* -------------------------------------------------------------------- */ @@ -644,10 +640,7 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) { config.quality = quality_factor; config.alpha_quality = alpha_quality_factor; config.method = method; -#if WEBP_ENCODER_ABI_VERSION >= 0x0209 - // the "exact" flag is only available in libwebp 0.5.0 and later config.exact = exact; -#endif // Validate the config if (!WebPValidateConfig(&config)) { @@ -763,124 +756,6 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) { Py_RETURN_NONE; } -PyObject * -WebPDecode_wrapper(PyObject *self, PyObject *args) { - PyBytesObject *webp_string; - const uint8_t *webp; - Py_ssize_t size; - PyObject *ret = Py_None, *bytes = NULL, *pymode = NULL, *icc_profile = NULL, - *exif = NULL; - WebPDecoderConfig config; - VP8StatusCode vp8_status_code = VP8_STATUS_OK; - char *mode = "RGB"; - - if (!PyArg_ParseTuple(args, "S", &webp_string)) { - return NULL; - } - - if (!WebPInitDecoderConfig(&config)) { - Py_RETURN_NONE; - } - - PyBytes_AsStringAndSize((PyObject *)webp_string, (char **)&webp, &size); - - vp8_status_code = WebPGetFeatures(webp, size, &config.input); - if (vp8_status_code == VP8_STATUS_OK) { - // If we don't set it, we don't get alpha. - // Initialized to MODE_RGB - if (config.input.has_alpha) { - config.output.colorspace = MODE_RGBA; - mode = "RGBA"; - } - - { - int copy_data = 0; - WebPData data = {webp, size}; - WebPMuxFrameInfo image; - WebPData icc_profile_data = {0}; - WebPData exif_data = {0}; - - WebPMux *mux = WebPMuxCreate(&data, copy_data); - if (NULL == mux) { - goto end; - } - - if (WEBP_MUX_OK != WebPMuxGetFrame(mux, 1, &image)) { - WebPMuxDelete(mux); - goto end; - } - - webp = image.bitstream.bytes; - size = image.bitstream.size; - - vp8_status_code = WebPDecode(webp, size, &config); - - if (WEBP_MUX_OK == WebPMuxGetChunk(mux, "ICCP", &icc_profile_data)) { - icc_profile = PyBytes_FromStringAndSize( - (const char *)icc_profile_data.bytes, icc_profile_data.size - ); - } - - if (WEBP_MUX_OK == WebPMuxGetChunk(mux, "EXIF", &exif_data)) { - exif = PyBytes_FromStringAndSize( - (const char *)exif_data.bytes, exif_data.size - ); - } - - WebPDataClear(&image.bitstream); - WebPMuxDelete(mux); - } - } - - if (vp8_status_code != VP8_STATUS_OK) { - goto end; - } - - if (config.output.colorspace < MODE_YUV) { - bytes = PyBytes_FromStringAndSize( - (char *)config.output.u.RGBA.rgba, config.output.u.RGBA.size - ); - } else { - // Skipping YUV for now. Need Test Images. - // UNDONE -- unclear if we'll ever get here if we set mode_rgb* - bytes = PyBytes_FromStringAndSize( - (char *)config.output.u.YUVA.y, config.output.u.YUVA.y_size - ); - } - - pymode = PyUnicode_FromString(mode); - ret = Py_BuildValue( - "SiiSSS", - bytes, - config.output.width, - config.output.height, - pymode, - NULL == icc_profile ? Py_None : icc_profile, - NULL == exif ? Py_None : exif - ); - -end: - WebPFreeDecBuffer(&config.output); - - Py_XDECREF(bytes); - Py_XDECREF(pymode); - Py_XDECREF(icc_profile); - Py_XDECREF(exif); - - if (Py_None == ret) { - Py_RETURN_NONE; - } - - return ret; -} - -// Return the decoder's version number, packed in hexadecimal using 8bits for -// each of major/minor/revision. E.g: v2.5.7 is 0x020507. -PyObject * -WebPDecoderVersion_wrapper() { - return Py_BuildValue("i", WebPGetDecoderVersion()); -} - // Version as string const char * WebPDecoderVersion_str(void) { @@ -901,39 +776,20 @@ WebPDecoderVersion_str(void) { /* -------------------------------------------------------------------- */ static PyMethodDef webpMethods[] = { -#ifdef HAVE_WEBPANIM {"WebPAnimDecoder", _anim_decoder_new, METH_VARARGS, "WebPAnimDecoder"}, {"WebPAnimEncoder", _anim_encoder_new, METH_VARARGS, "WebPAnimEncoder"}, -#endif {"WebPEncode", WebPEncode_wrapper, METH_VARARGS, "WebPEncode"}, - {"WebPDecode", WebPDecode_wrapper, METH_VARARGS, "WebPDecode"}, - {"WebPDecoderVersion", WebPDecoderVersion_wrapper, METH_NOARGS, "WebPVersion"}, {NULL, NULL} }; -void -addAnimFlagToModule(PyObject *m) { - PyObject *have_webpanim; -#ifdef HAVE_WEBPANIM - have_webpanim = Py_True; -#else - have_webpanim = Py_False; -#endif - Py_INCREF(have_webpanim); - PyModule_AddObject(m, "HAVE_WEBPANIM", have_webpanim); -} - static int setup_module(PyObject *m) { -#ifdef HAVE_WEBPANIM + PyObject *d = PyModule_GetDict(m); /* Ready object types */ if (PyType_Ready(&WebPAnimDecoder_Type) < 0 || PyType_Ready(&WebPAnimEncoder_Type) < 0) { return -1; } -#endif - PyObject *d = PyModule_GetDict(m); - addAnimFlagToModule(m); PyObject *v = PyUnicode_FromString(WebPDecoderVersion_str()); PyDict_SetItemString(d, "webpdecoder_version", v ? v : Py_None); From 56ca359c657ee2f4e81426f780c4b4e2e281e404 Mon Sep 17 00:00:00 2001 From: Aleksandr Karpinskii Date: Mon, 8 Jul 2024 20:07:43 +0400 Subject: [PATCH 04/14] Bring back removed features, add deprecations and Release notes --- Tests/check_wheel.py | 3 +++ Tests/test_features.py | 12 ++++++++++++ docs/reference/features.rst | 3 +++ docs/releasenotes/11.0.0.rst | 8 ++++++++ src/PIL/features.py | 10 +++++++++- 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Tests/check_wheel.py b/Tests/check_wheel.py index 8fcb75dad5c..4b91984f58a 100644 --- a/Tests/check_wheel.py +++ b/Tests/check_wheel.py @@ -27,6 +27,9 @@ def test_wheel_codecs() -> None: def test_wheel_features() -> None: expected_features = { + "webp_anim", + "webp_mux", + "transp_webp", "raqm", "fribidi", "harfbuzz", diff --git a/Tests/test_features.py b/Tests/test_features.py index 40466082f7a..cb8fd8688c9 100644 --- a/Tests/test_features.py +++ b/Tests/test_features.py @@ -46,6 +46,18 @@ def test(name: str, function: Callable[[str], str | None]) -> None: test(feature, features.version_feature) +def test_webp_transparency() -> None: + assert features.check("transp_webp") == features.check_module("webp") + + +def test_webp_mux() -> None: + assert features.check("webp_mux") == features.check_module("webp") + + +def test_webp_anim() -> None: + assert features.check("webp_anim") == features.check_module("webp") + + @skip_unless_feature("libjpeg_turbo") def test_libjpeg_turbo_version() -> None: version = features.version("libjpeg_turbo") diff --git a/docs/reference/features.rst b/docs/reference/features.rst index 26c8ab8cd69..fcff9673567 100644 --- a/docs/reference/features.rst +++ b/docs/reference/features.rst @@ -57,6 +57,9 @@ Support for the following features can be checked: * ``raqm``: Raqm library, required for ``ImageFont.Layout.RAQM`` in :py:func:`PIL.ImageFont.truetype`. Run-time version number is available for Raqm 0.7.0 or newer. * ``libimagequant``: (compile time) ImageQuant quantization support in :py:func:`PIL.Image.Image.quantize`. Run-time version number is available. * ``xcb``: (compile time) Support for X11 in :py:func:`PIL.ImageGrab.grab` via the XCB library. +* ``transp_webp``: Deprecated. Always ``True`` if WebP module is installed. +* ``webp_mux``: Deprecated. Always ``True`` if WebP module is installed. +* ``webp_anim``: Deprecated. Always ``True`` if WebP module is installed. .. autofunction:: PIL.features.check_feature .. autofunction:: PIL.features.version_feature diff --git a/docs/releasenotes/11.0.0.rst b/docs/releasenotes/11.0.0.rst index 1d1afcde53a..bb707a04480 100644 --- a/docs/releasenotes/11.0.0.rst +++ b/docs/releasenotes/11.0.0.rst @@ -58,6 +58,14 @@ JpegImageFile.huffman_ac and JpegImageFile.huffman_dc The ``huffman_ac`` and ``huffman_dc`` dictionaries on JPEG images were unused. They have been deprecated, and will be removed in Pillow 12 (2025-10-15). +Specific WebP Feature Checks +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The following features ``features.check("transp_webp")``, +``features.check("webp_mux")``, and ``features.check("webp_anim")`` are now +always ``True`` if the WebP module is installed and should not be used. +These checks will be removed in Pillow 12.0.0 (2025-10-15). + API Changes =========== diff --git a/src/PIL/features.py b/src/PIL/features.py index bbf7c641b43..f594a2cdc4a 100644 --- a/src/PIL/features.py +++ b/src/PIL/features.py @@ -7,6 +7,7 @@ from typing import IO import PIL +from PIL import _deprecate from . import Image @@ -119,6 +120,9 @@ def get_supported_codecs() -> list[str]: features = { + "webp_anim": ("PIL._webp", True, None), + "webp_mux": ("PIL._webp", True, None), + "transp_webp": ("PIL._webp", True, None), "raqm": ("PIL._imagingft", "HAVE_RAQM", "raqm_version"), "fribidi": ("PIL._imagingft", "HAVE_FRIBIDI", "fribidi_version"), "harfbuzz": ("PIL._imagingft", "HAVE_HARFBUZZ", "harfbuzz_version"), @@ -144,7 +148,11 @@ def check_feature(feature: str) -> bool | None: try: imported_module = __import__(module, fromlist=["PIL"]) - return getattr(imported_module, flag) + if isinstance(flag, str): + return getattr(imported_module, flag) + else: + _deprecate.deprecate(f'check_feature("{feature}")', 12) + return flag except ModuleNotFoundError: return None except ImportError as ex: From 6180abc75c3dfa950276b2586cd1a8aab1bede70 Mon Sep 17 00:00:00 2001 From: Aleksandr Karpinskii Date: Mon, 8 Jul 2024 20:32:35 +0400 Subject: [PATCH 05/14] Remove WebP versions notes from docs --- docs/handbook/image-file-formats.rst | 1 - docs/installation/building-from-source.rst | 4 ---- 2 files changed, 5 deletions(-) diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index 49a17bb5c4f..861e09a43aa 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -1248,7 +1248,6 @@ The :py:meth:`~PIL.Image.Image.save` method supports the following options: **exact** 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. **icc_profile** The ICC Profile to include in the saved file. diff --git a/docs/installation/building-from-source.rst b/docs/installation/building-from-source.rst index f47edbc5bcc..71787311f35 100644 --- a/docs/installation/building-from-source.rst +++ b/docs/installation/building-from-source.rst @@ -55,10 +55,6 @@ Many of Pillow's features require external libraries: * **libwebp** provides the WebP format. - * Pillow has been tested with version **0.1.3**, which does not read - transparent WebP files. Versions **0.3.0** and above support - transparency. - * **openjpeg** provides JPEG 2000 functionality. * Pillow has been tested with openjpeg **2.0.0**, **2.1.0**, **2.3.1**, From 924df9e60b39c6ced91553895497fa43db2d232d Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 13 Aug 2024 06:46:46 +1000 Subject: [PATCH 06/14] Moved line after early return Improve compiler advice Update src/PIL/features.py --- src/PIL/features.py | 5 ++--- src/_webp.c | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/PIL/features.py b/src/PIL/features.py index f594a2cdc4a..a5487042ead 100644 --- a/src/PIL/features.py +++ b/src/PIL/features.py @@ -148,11 +148,10 @@ def check_feature(feature: str) -> bool | None: try: imported_module = __import__(module, fromlist=["PIL"]) - if isinstance(flag, str): - return getattr(imported_module, flag) - else: + if isinstance(flag, bool): _deprecate.deprecate(f'check_feature("{feature}")', 12) return flag + return getattr(imported_module, flag) except ModuleNotFoundError: return None except ImportError as ex: diff --git a/src/_webp.c b/src/_webp.c index 8302925b0fc..306a290bdb9 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -14,7 +14,7 @@ * versions, before enabling animation support. */ #if WEBP_MUX_ABI_VERSION < 0x0106 || WEBP_DEMUX_ABI_VERSION < 0x0107 -#error libwebp 0.5.0 and above is required. Upgrade libwebp or build with --disable-webp flag +#error libwebp 0.5.0 and above is required. Upgrade libwebp or build Pillow with --disable-webp flag #endif void @@ -784,13 +784,13 @@ static PyMethodDef webpMethods[] = { static int setup_module(PyObject *m) { - PyObject *d = PyModule_GetDict(m); /* Ready object types */ if (PyType_Ready(&WebPAnimDecoder_Type) < 0 || PyType_Ready(&WebPAnimEncoder_Type) < 0) { return -1; } + PyObject *d = PyModule_GetDict(m); PyObject *v = PyUnicode_FromString(WebPDecoderVersion_str()); PyDict_SetItemString(d, "webpdecoder_version", v ? v : Py_None); Py_XDECREF(v); From 93ce9ce0048639706eecd523fc54c7d6cfc15bef Mon Sep 17 00:00:00 2001 From: Alexander Karpinsky Date: Tue, 13 Aug 2024 09:52:07 +0400 Subject: [PATCH 07/14] Update features type Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- src/PIL/features.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PIL/features.py b/src/PIL/features.py index a5487042ead..9421dbd3cf4 100644 --- a/src/PIL/features.py +++ b/src/PIL/features.py @@ -119,7 +119,7 @@ def get_supported_codecs() -> list[str]: return [f for f in codecs if check_codec(f)] -features = { +features: dict[str, tuple[str, str | bool, str | None]] = { "webp_anim": ("PIL._webp", True, None), "webp_mux": ("PIL._webp", True, None), "transp_webp": ("PIL._webp", True, None), From 55469948282fe2dd763dcb8e66a767b5c63adb54 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 13 Aug 2024 19:03:14 +1000 Subject: [PATCH 08/14] Removed unnecessary variable --- Tests/test_file_webp_alpha.py | 2 +- Tests/test_file_webp_lossless.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_webp_alpha.py b/Tests/test_file_webp_alpha.py index 53cdfff0327..c88fe3589ea 100644 --- a/Tests/test_file_webp_alpha.py +++ b/Tests/test_file_webp_alpha.py @@ -13,7 +13,7 @@ hopper, ) -_webp = pytest.importorskip("PIL._webp", reason="WebP support not installed") +pytest.importorskip("PIL._webp", reason="WebP support not installed") def test_read_rgba() -> None: diff --git a/Tests/test_file_webp_lossless.py b/Tests/test_file_webp_lossless.py index dc98ad3f013..80429715e9e 100644 --- a/Tests/test_file_webp_lossless.py +++ b/Tests/test_file_webp_lossless.py @@ -8,7 +8,7 @@ from .helper import assert_image_equal, hopper -_webp = pytest.importorskip("PIL._webp", reason="WebP support not installed") +pytest.importorskip("PIL._webp", reason="WebP support not installed") RGB_MODE = "RGB" From f3aec6dd383bb27676e7e02a7749a5bff1cc30c2 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 13 Aug 2024 19:05:32 +1000 Subject: [PATCH 09/14] Simplified code --- setup.py | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/setup.py b/setup.py index edfa9ee28fc..863d102cf85 100644 --- a/setup.py +++ b/setup.py @@ -794,25 +794,17 @@ def build_extensions(self): if feature.want("webp"): _dbg("Looking for webp") if all( - _find_include_file(self, src) - for src in [ - "webp/encode.h", - "webp/decode.h", - "webp/mux.h", - "webp/demux.h", - ] + _find_include_file(self, "webp/" + include) + for include in ("encode.h", "decode.h", "mux.h", "demux.h") ): - # In Google's precompiled zip it is call "libwebp": - if all( - _find_library_file(self, lib) - for lib in ["webp", "webpmux", "webpdemux"] - ): - feature.webp = "webp" - elif all( - _find_library_file(self, lib) - for lib in ["libwebp", "libwebpmux", "libwebpdemux"] - ): - feature.webp = "libwebp" + # In Google's precompiled zip it is called "libwebp" + for prefix in ("", "lib"): + if all( + _find_library_file(self, prefix + library) + for library in ("webp", "webpmux", "webpdemux") + ): + feature.webp = prefix + "webp" + break if feature.want("xcb"): _dbg("Looking for xcb") @@ -901,12 +893,8 @@ def build_extensions(self): self._remove_extension("PIL._imagingcms") if feature.webp: - libs = [ - feature.webp, - feature.webp + "mux", - feature.webp + "demux", - ] - self._update_extension("PIL._webp", libs, []) + libs = [feature.webp, feature.webp + "mux", feature.webp + "demux"] + self._update_extension("PIL._webp", libs) else: self._remove_extension("PIL._webp") From c7e6289b36d4eb1823b6bfcace27674826a58e43 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 13 Aug 2024 19:07:01 +1000 Subject: [PATCH 10/14] Use relative import --- src/PIL/features.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PIL/features.py b/src/PIL/features.py index 9421dbd3cf4..e505adae060 100644 --- a/src/PIL/features.py +++ b/src/PIL/features.py @@ -7,9 +7,9 @@ from typing import IO import PIL -from PIL import _deprecate from . import Image +from ._deprecate import deprecate modules = { "pil": ("PIL._imaging", "PILLOW_VERSION"), @@ -149,7 +149,7 @@ def check_feature(feature: str) -> bool | None: try: imported_module = __import__(module, fromlist=["PIL"]) if isinstance(flag, bool): - _deprecate.deprecate(f'check_feature("{feature}")', 12) + deprecate(f'check_feature("{feature}")', 12) return flag return getattr(imported_module, flag) except ModuleNotFoundError: From ba82dff7bc6c489f299a63d2f2d3722dfe11852e Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 13 Aug 2024 19:10:54 +1000 Subject: [PATCH 11/14] Updated test name --- Tests/test_file_webp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 6e9eea4aec0..6ccd489bb47 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -166,7 +166,7 @@ def test_WebPEncode_with_invalid_args(self) -> None: with pytest.raises(TypeError): _webp.WebPEncode() - def test_WebPDecode_with_invalid_args(self) -> None: + def test_WebPAnimDecoder_with_invalid_args(self) -> None: """ Calling decoder functions with no arguments should result in an error. """ From 66319fcce764f4f09e53900b2896ec15457f691f Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 13 Aug 2024 19:17:36 +1000 Subject: [PATCH 12/14] Animation support is no longer conditionally enabled --- src/_webp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_webp.c b/src/_webp.c index 306a290bdb9..d1943b3e0a5 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -11,7 +11,7 @@ * Check the versions from mux.h and demux.h, to ensure the WebPAnimEncoder and * WebPAnimDecoder APIs are present (initial support was added in 0.5.0). The * very early versions had some significant differences, so we require later - * versions, before enabling animation support. + * versions. */ #if WEBP_MUX_ABI_VERSION < 0x0106 || WEBP_DEMUX_ABI_VERSION < 0x0107 #error libwebp 0.5.0 and above is required. Upgrade libwebp or build Pillow with --disable-webp flag From 45552b5b4f6ea470639e5c7ff0d9efb96ad7db68 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 13 Aug 2024 19:24:34 +1000 Subject: [PATCH 13/14] Updated documentation --- docs/deprecations.rst | 10 ++++++++++ docs/releasenotes/11.0.0.rst | 8 ++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/docs/deprecations.rst b/docs/deprecations.rst index 058468cfe36..a9498d5ed5e 100644 --- a/docs/deprecations.rst +++ b/docs/deprecations.rst @@ -126,6 +126,16 @@ JpegImageFile.huffman_ac and JpegImageFile.huffman_dc The ``huffman_ac`` and ``huffman_dc`` dictionaries on JPEG images were unused. They have been deprecated, and will be removed in Pillow 12 (2025-10-15). +Specific WebP Feature Checks +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. deprecated:: 11.0.0 + +``features.check("transp_webp")``, ``features.check("webp_mux")`` and +``features.check("webp_anim")`` are now deprecated. They will always return +``True`` if the WebP module is installed, until they are removed in Pillow +12.0.0 (2025-10-15). + Removed features ---------------- diff --git a/docs/releasenotes/11.0.0.rst b/docs/releasenotes/11.0.0.rst index bb707a04480..ac9237acf78 100644 --- a/docs/releasenotes/11.0.0.rst +++ b/docs/releasenotes/11.0.0.rst @@ -61,10 +61,10 @@ have been deprecated, and will be removed in Pillow 12 (2025-10-15). Specific WebP Feature Checks ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -The following features ``features.check("transp_webp")``, -``features.check("webp_mux")``, and ``features.check("webp_anim")`` are now -always ``True`` if the WebP module is installed and should not be used. -These checks will be removed in Pillow 12.0.0 (2025-10-15). +``features.check("transp_webp")``, ``features.check("webp_mux")`` and +``features.check("webp_anim")`` are now deprecated. They will always return +``True`` if the WebP module is installed, until they are removed in Pillow +12.0.0 (2025-10-15). API Changes =========== From 359d7592c7861729092434154d131310c7bf1d71 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 14 Aug 2024 18:41:39 +1000 Subject: [PATCH 14/14] Test deprecation warnings --- Tests/test_features.py | 21 ++++++++++++++++----- src/PIL/features.py | 12 +++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Tests/test_features.py b/Tests/test_features.py index cb8fd8688c9..80778284773 100644 --- a/Tests/test_features.py +++ b/Tests/test_features.py @@ -18,7 +18,11 @@ def test_check() -> None: for codec in features.codecs: assert features.check_codec(codec) == features.check(codec) for feature in features.features: - assert features.check_feature(feature) == features.check(feature) + if "webp" in feature: + with pytest.warns(DeprecationWarning): + assert features.check_feature(feature) == features.check(feature) + else: + assert features.check_feature(feature) == features.check(feature) def test_version() -> None: @@ -43,19 +47,26 @@ def test(name: str, function: Callable[[str], str | None]) -> None: for codec in features.codecs: test(codec, features.version_codec) for feature in features.features: - test(feature, features.version_feature) + if "webp" in feature: + with pytest.warns(DeprecationWarning): + test(feature, features.version_feature) + else: + test(feature, features.version_feature) def test_webp_transparency() -> None: - assert features.check("transp_webp") == features.check_module("webp") + with pytest.warns(DeprecationWarning): + assert features.check("transp_webp") == features.check_module("webp") def test_webp_mux() -> None: - assert features.check("webp_mux") == features.check_module("webp") + with pytest.warns(DeprecationWarning): + assert features.check("webp_mux") == features.check_module("webp") def test_webp_anim() -> None: - assert features.check("webp_anim") == features.check_module("webp") + with pytest.warns(DeprecationWarning): + assert features.check("webp_anim") == features.check_module("webp") @skip_unless_feature("libjpeg_turbo") diff --git a/src/PIL/features.py b/src/PIL/features.py index e505adae060..24c5ee978b3 100644 --- a/src/PIL/features.py +++ b/src/PIL/features.py @@ -180,7 +180,17 @@ def get_supported_features() -> list[str]: """ :returns: A list of all supported features. """ - return [f for f in features if check_feature(f)] + supported_features = [] + for f, (module, flag, _) in features.items(): + if flag is True: + for feature, (feature_module, _) in modules.items(): + if feature_module == module: + if check_module(feature): + supported_features.append(f) + break + elif check_feature(f): + supported_features.append(f) + return supported_features def check(feature: str) -> bool | None: