2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2020-04-11 13:43:49 +03:00
|
|
|
import io
|
2020-06-15 16:32:30 +03:00
|
|
|
import re
|
2023-06-27 07:43:58 +03:00
|
|
|
import sys
|
2022-02-21 05:49:01 +03:00
|
|
|
import warnings
|
2024-01-31 12:12:58 +03:00
|
|
|
from pathlib import Path
|
2020-04-11 13:43:49 +03:00
|
|
|
|
2020-02-03 12:11:32 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2020-06-15 16:32:30 +03:00
|
|
|
from PIL import Image, WebPImagePlugin, features
|
2013-03-19 22:43:45 +04:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
from .helper import (
|
2022-03-17 15:49:23 +03:00
|
|
|
assert_image_equal,
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar,
|
|
|
|
assert_image_similar_tofile,
|
|
|
|
hopper,
|
2020-02-18 01:03:32 +03:00
|
|
|
skip_unless_feature,
|
2020-01-30 17:56:07 +03:00
|
|
|
)
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2013-03-27 21:00:35 +04:00
|
|
|
try:
|
2013-04-09 08:53:59 +04:00
|
|
|
from PIL import _webp
|
2019-06-13 18:54:11 +03:00
|
|
|
|
2017-09-28 05:28:43 +03:00
|
|
|
HAVE_WEBP = True
|
2014-10-01 17:50:33 +04:00
|
|
|
except ImportError:
|
2017-09-28 05:28:43 +03:00
|
|
|
HAVE_WEBP = False
|
2013-03-19 22:43:45 +04:00
|
|
|
|
2017-10-02 01:23:18 +03:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
class TestUnsupportedWebp:
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_unsupported(self) -> None:
|
2018-09-30 08:34:27 +03:00
|
|
|
if HAVE_WEBP:
|
|
|
|
WebPImagePlugin.SUPPORTED = False
|
|
|
|
|
|
|
|
file_path = "Tests/images/hopper.webp"
|
2023-02-23 16:30:38 +03:00
|
|
|
with pytest.warns(UserWarning):
|
|
|
|
with pytest.raises(OSError):
|
2023-03-03 17:00:56 +03:00
|
|
|
with Image.open(file_path):
|
|
|
|
pass
|
2018-09-30 08:34:27 +03:00
|
|
|
|
|
|
|
if HAVE_WEBP:
|
|
|
|
WebPImagePlugin.SUPPORTED = True
|
|
|
|
|
2018-10-18 10:32:17 +03:00
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("webp")
|
2020-03-22 22:54:54 +03:00
|
|
|
class TestFileWebp:
|
2024-01-31 12:12:58 +03:00
|
|
|
def setup_method(self) -> None:
|
2018-10-18 10:32:17 +03:00
|
|
|
self.rgb_mode = "RGB"
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_version(self) -> None:
|
2014-06-10 13:10:47 +04:00
|
|
|
_webp.WebPDecoderVersion()
|
|
|
|
_webp.WebPDecoderBuggyAlpha()
|
2024-05-30 05:00:50 +03:00
|
|
|
version = features.version_module("webp")
|
|
|
|
assert version is not None
|
|
|
|
assert re.search(r"\d+\.\d+\.\d+$", version)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_read_rgb(self) -> None:
|
2017-09-27 06:27:40 +03:00
|
|
|
"""
|
2017-10-02 01:23:18 +03:00
|
|
|
Can we read a RGB mode WebP file without error?
|
2017-09-27 06:27:40 +03:00
|
|
|
Does it have the bits we expect?
|
|
|
|
"""
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/hopper.webp") as image:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert image.mode == self.rgb_mode
|
|
|
|
assert image.size == (128, 128)
|
|
|
|
assert image.format == "WEBP"
|
2019-11-25 23:03:23 +03:00
|
|
|
image.load()
|
|
|
|
image.getdata()
|
|
|
|
|
|
|
|
# generated with:
|
|
|
|
# dwebp -ppm ../../Tests/images/hopper.webp -o hopper_webp_bits.ppm
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar_tofile(image, "Tests/images/hopper_webp_bits.ppm", 1.0)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def _roundtrip(self, tmp_path: Path, mode, epsilon, args={}) -> None:
|
2020-03-22 22:54:54 +03:00
|
|
|
temp_file = str(tmp_path / "temp.webp")
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2020-04-11 13:43:49 +03:00
|
|
|
hopper(mode).save(temp_file, **args)
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(temp_file) as image:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert image.mode == self.rgb_mode
|
|
|
|
assert image.size == (128, 128)
|
|
|
|
assert image.format == "WEBP"
|
2019-11-25 23:03:23 +03:00
|
|
|
image.load()
|
|
|
|
image.getdata()
|
|
|
|
|
2020-04-11 13:43:49 +03:00
|
|
|
if mode == self.rgb_mode:
|
|
|
|
# generated with: dwebp -ppm temp.webp -o hopper_webp_write.ppm
|
|
|
|
assert_image_similar_tofile(
|
|
|
|
image, "Tests/images/hopper_webp_write.ppm", 12.0
|
|
|
|
)
|
2019-11-25 23:03:23 +03:00
|
|
|
|
|
|
|
# This test asserts that the images are similar. If the average pixel
|
|
|
|
# difference between the two images is less than the epsilon value,
|
|
|
|
# then we're going to accept that it's a reasonable lossy version of
|
2020-04-11 13:43:49 +03:00
|
|
|
# the image.
|
|
|
|
target = hopper(mode)
|
|
|
|
if mode != self.rgb_mode:
|
|
|
|
target = target.convert(self.rgb_mode)
|
|
|
|
assert_image_similar(image, target, epsilon)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_write_rgb(self, tmp_path: Path) -> None:
|
2020-04-11 13:43:49 +03:00
|
|
|
"""
|
|
|
|
Can we write a RGB mode file to webp without error?
|
|
|
|
Does it have the bits we expect?
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._roundtrip(tmp_path, self.rgb_mode, 12.5)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_write_method(self, tmp_path: Path) -> None:
|
2020-04-11 13:43:49 +03:00
|
|
|
self._roundtrip(tmp_path, self.rgb_mode, 12.0, {"method": 6})
|
|
|
|
|
|
|
|
buffer_no_args = io.BytesIO()
|
|
|
|
hopper().save(buffer_no_args, format="WEBP")
|
|
|
|
|
|
|
|
buffer_method = io.BytesIO()
|
|
|
|
hopper().save(buffer_method, format="WEBP", method=6)
|
|
|
|
assert buffer_no_args.getbuffer() != buffer_method.getbuffer()
|
2017-09-27 06:27:40 +03:00
|
|
|
|
2022-03-17 15:49:23 +03:00
|
|
|
@skip_unless_feature("webp_anim")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_save_all(self, tmp_path: Path) -> None:
|
2022-03-17 15:49:23 +03:00
|
|
|
temp_file = str(tmp_path / "temp.webp")
|
|
|
|
im = Image.new("RGB", (1, 1))
|
|
|
|
im2 = Image.new("RGB", (1, 1), "#f00")
|
|
|
|
im.save(temp_file, save_all=True, append_images=[im2])
|
|
|
|
|
|
|
|
with Image.open(temp_file) as reloaded:
|
|
|
|
assert_image_equal(im, reloaded)
|
|
|
|
|
|
|
|
reloaded.seek(1)
|
|
|
|
assert_image_similar(im2, reloaded, 1)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_icc_profile(self, tmp_path: Path) -> None:
|
2021-07-10 08:15:10 +03:00
|
|
|
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}
|
|
|
|
)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_write_unsupported_mode_L(self, tmp_path: Path) -> None:
|
2017-09-27 06:27:40 +03:00
|
|
|
"""
|
2017-10-02 01:23:18 +03:00
|
|
|
Saving a black-and-white file to WebP format should work, and be
|
2017-09-27 06:27:40 +03:00
|
|
|
similar to the original file.
|
|
|
|
"""
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2020-04-11 13:43:49 +03:00
|
|
|
self._roundtrip(tmp_path, "L", 10.0)
|
2017-09-27 06:27:40 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_write_unsupported_mode_P(self, tmp_path: Path) -> None:
|
2017-09-27 06:27:40 +03:00
|
|
|
"""
|
2017-10-02 01:23:18 +03:00
|
|
|
Saving a palette-based file to WebP format should work, and be
|
2017-09-27 06:27:40 +03:00
|
|
|
similar to the original file.
|
|
|
|
"""
|
|
|
|
|
2020-04-11 13:43:49 +03:00
|
|
|
self._roundtrip(tmp_path, "P", 50.0)
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2023-06-27 07:43:58 +03:00
|
|
|
@pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_write_encoding_error_message(self, tmp_path: Path) -> None:
|
2021-05-06 17:12:03 +03:00
|
|
|
temp_file = str(tmp_path / "temp.webp")
|
|
|
|
im = Image.new("RGB", (15000, 15000))
|
|
|
|
with pytest.raises(ValueError) as e:
|
|
|
|
im.save(temp_file, method=0)
|
|
|
|
assert str(e.value) == "encoding error 6"
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_WebPEncode_with_invalid_args(self) -> None:
|
2017-09-27 06:27:40 +03:00
|
|
|
"""
|
|
|
|
Calling encoder functions with no arguments should result in an error.
|
|
|
|
"""
|
|
|
|
|
2017-09-28 06:21:18 +03:00
|
|
|
if _webp.HAVE_WEBPANIM:
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
_webp.WebPAnimEncoder()
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
_webp.WebPEncode()
|
2017-05-12 18:11:58 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_WebPDecode_with_invalid_args(self) -> None:
|
2017-09-27 06:27:40 +03:00
|
|
|
"""
|
|
|
|
Calling decoder functions with no arguments should result in an error.
|
|
|
|
"""
|
|
|
|
|
2017-09-28 06:21:18 +03:00
|
|
|
if _webp.HAVE_WEBPANIM:
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(TypeError):
|
|
|
|
_webp.WebPAnimDecoder()
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
_webp.WebPDecode()
|
2013-10-22 23:32:31 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_no_resource_warning(self, tmp_path: Path) -> None:
|
2018-09-08 16:08:17 +03:00
|
|
|
file_path = "Tests/images/hopper.webp"
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(file_path) as image:
|
2020-03-22 22:54:54 +03:00
|
|
|
temp_file = str(tmp_path / "temp.webp")
|
2022-02-21 05:49:01 +03:00
|
|
|
with warnings.catch_warnings():
|
2021-02-10 15:37:55 +03:00
|
|
|
image.save(temp_file)
|
2018-09-08 16:08:17 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_file_pointer_could_be_reused(self) -> None:
|
2018-10-18 10:32:17 +03:00
|
|
|
file_path = "Tests/images/hopper.webp"
|
2019-06-13 18:54:11 +03:00
|
|
|
with open(file_path, "rb") as blob:
|
2018-10-18 10:32:17 +03:00
|
|
|
Image.open(blob).load()
|
|
|
|
Image.open(blob).load()
|
|
|
|
|
2022-04-26 11:55:54 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"background",
|
|
|
|
(0, (0,), (-1, 0, 1, 2), (253, 254, 255, 256)),
|
|
|
|
)
|
|
|
|
@skip_unless_feature("webp_anim")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_invalid_background(self, background, tmp_path: Path) -> None:
|
2022-04-26 11:55:54 +03:00
|
|
|
temp_file = str(tmp_path / "temp.webp")
|
|
|
|
im = hopper()
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
im.save(temp_file, save_all=True, append_images=[im], background=background)
|
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("webp_anim")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_background_from_gif(self, tmp_path: Path) -> None:
|
2022-03-21 12:09:53 +03:00
|
|
|
# Save L mode GIF with background
|
|
|
|
with Image.open("Tests/images/no_palette_with_background.gif") as im:
|
|
|
|
out_webp = str(tmp_path / "temp.webp")
|
|
|
|
im.save(out_webp, save_all=True)
|
|
|
|
|
|
|
|
# Save P mode GIF with background
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open("Tests/images/chi.gif") as im:
|
|
|
|
original_value = im.convert("RGB").getpixel((1, 1))
|
2018-11-20 11:50:14 +03:00
|
|
|
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
# Save as WEBP
|
2020-03-22 22:54:54 +03:00
|
|
|
out_webp = str(tmp_path / "temp.webp")
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
im.save(out_webp, save_all=True)
|
2018-11-20 11:50:14 +03:00
|
|
|
|
|
|
|
# Save as GIF
|
2020-03-22 22:54:54 +03:00
|
|
|
out_gif = str(tmp_path / "temp.gif")
|
2021-02-11 13:43:54 +03:00
|
|
|
with Image.open(out_webp) as im:
|
|
|
|
im.save(out_gif)
|
2018-11-20 11:50:14 +03:00
|
|
|
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with Image.open(out_gif) as reread:
|
|
|
|
reread_value = reread.convert("RGB").getpixel((1, 1))
|
2021-10-15 13:10:22 +03:00
|
|
|
difference = sum(abs(original_value[i] - reread_value[i]) for i in range(0, 3))
|
2020-02-22 16:06:21 +03:00
|
|
|
assert difference < 5
|
2021-03-17 12:37:31 +03:00
|
|
|
|
|
|
|
@skip_unless_feature("webp_anim")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_duration(self, tmp_path: Path) -> None:
|
2021-03-17 12:37:31 +03:00
|
|
|
with Image.open("Tests/images/dispose_bgnd.gif") as im:
|
|
|
|
assert im.info["duration"] == 1000
|
|
|
|
|
|
|
|
out_webp = str(tmp_path / "temp.webp")
|
|
|
|
im.save(out_webp, save_all=True)
|
|
|
|
|
|
|
|
with Image.open(out_webp) as reloaded:
|
2023-09-18 13:34:05 +03:00
|
|
|
reloaded.load()
|
2021-03-17 12:37:31 +03:00
|
|
|
assert reloaded.info["duration"] == 1000
|
2023-09-25 13:28:25 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_roundtrip_rgba_palette(self, tmp_path: Path) -> None:
|
2023-09-25 13:28:25 +03:00
|
|
|
temp_file = str(tmp_path / "temp.webp")
|
|
|
|
im = Image.new("RGBA", (1, 1)).convert("P")
|
|
|
|
assert im.mode == "P"
|
|
|
|
assert im.palette.mode == "RGBA"
|
|
|
|
im.save(temp_file)
|
|
|
|
|
|
|
|
with Image.open(temp_file) as im:
|
|
|
|
assert im.getpixel((0, 0)) == (0, 0, 0, 0)
|