2020-01-05 00:07:59 +03:00
|
|
|
import pytest
|
2017-09-27 06:27:40 +03:00
|
|
|
from PIL import Image
|
|
|
|
|
2020-01-05 00:07:59 +03:00
|
|
|
from .helper import PillowTestCase, is_big_endian, on_ci
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2017-09-27 06:27:40 +03:00
|
|
|
try:
|
|
|
|
from PIL import _webp
|
2019-06-13 18:54:11 +03:00
|
|
|
|
2017-09-28 05:28:43 +03:00
|
|
|
HAVE_WEBP = True
|
2017-09-27 06:27:40 +03:00
|
|
|
except ImportError:
|
2017-09-28 05:28:43 +03:00
|
|
|
HAVE_WEBP = False
|
2017-09-27 06:27:40 +03:00
|
|
|
|
2017-10-02 01:23:18 +03:00
|
|
|
|
2017-09-27 06:27:40 +03:00
|
|
|
class TestFileWebpAnimation(PillowTestCase):
|
|
|
|
def setUp(self):
|
2017-09-28 05:28:43 +03:00
|
|
|
if not HAVE_WEBP:
|
2019-06-13 18:54:11 +03:00
|
|
|
self.skipTest("WebP support not installed")
|
2017-09-28 05:28:43 +03:00
|
|
|
return
|
2017-09-27 06:27:40 +03:00
|
|
|
|
2017-09-28 05:04:24 +03:00
|
|
|
if not _webp.HAVE_WEBPANIM:
|
2019-06-13 18:54:11 +03:00
|
|
|
self.skipTest(
|
|
|
|
"WebP library does not contain animation support, "
|
|
|
|
"not testing animation"
|
|
|
|
)
|
2017-09-27 06:27:40 +03:00
|
|
|
|
|
|
|
def test_n_frames(self):
|
|
|
|
"""
|
2017-10-02 01:23:18 +03:00
|
|
|
Ensure that WebP format sets n_frames and is_animated
|
2017-09-27 06:27:40 +03:00
|
|
|
attributes correctly.
|
|
|
|
"""
|
|
|
|
|
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/hopper.webp") as im:
|
|
|
|
self.assertEqual(im.n_frames, 1)
|
|
|
|
self.assertFalse(im.is_animated)
|
2017-09-27 06:27:40 +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("Tests/images/iss634.webp") as im:
|
|
|
|
self.assertEqual(im.n_frames, 42)
|
|
|
|
self.assertTrue(im.is_animated)
|
2017-09-27 06:27:40 +03:00
|
|
|
|
2020-01-05 00:07:59 +03:00
|
|
|
@pytest.mark.xfail(is_big_endian() and on_ci(), reason="Fails on big-endian")
|
2017-09-28 07:22:05 +03:00
|
|
|
def test_write_animation_L(self):
|
2017-09-27 06:27:40 +03:00
|
|
|
"""
|
|
|
|
Convert an animated GIF to animated WebP, then compare the
|
|
|
|
frame count, and first and last frames to ensure they're
|
|
|
|
visually similar.
|
|
|
|
"""
|
|
|
|
|
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/iss634.gif") as orig:
|
|
|
|
self.assertGreater(orig.n_frames, 1)
|
2017-09-27 06:27:40 +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
|
|
|
temp_file = self.tempfile("temp.webp")
|
|
|
|
orig.save(temp_file, save_all=True)
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(temp_file) as im:
|
|
|
|
self.assertEqual(im.n_frames, orig.n_frames)
|
|
|
|
|
|
|
|
# Compare first and last frames to the original animated GIF
|
|
|
|
orig.load()
|
|
|
|
im.load()
|
|
|
|
self.assert_image_similar(im, orig.convert("RGBA"), 25.0)
|
|
|
|
orig.seek(orig.n_frames - 1)
|
|
|
|
im.seek(im.n_frames - 1)
|
|
|
|
orig.load()
|
|
|
|
im.load()
|
|
|
|
self.assert_image_similar(im, orig.convert("RGBA"), 25.0)
|
2017-09-27 06:27:40 +03:00
|
|
|
|
2020-01-05 00:07:59 +03:00
|
|
|
@pytest.mark.xfail(is_big_endian() and on_ci(), reason="Fails on big-endian")
|
2017-09-28 07:22:05 +03:00
|
|
|
def test_write_animation_RGB(self):
|
|
|
|
"""
|
|
|
|
Write an animated WebP from RGB frames, and ensure the frames
|
|
|
|
are visually similar to the originals.
|
|
|
|
"""
|
|
|
|
|
2017-11-06 12:06:50 +03:00
|
|
|
def check(temp_file):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(temp_file) as im:
|
|
|
|
self.assertEqual(im.n_frames, 2)
|
|
|
|
|
|
|
|
# Compare first frame to original
|
|
|
|
im.load()
|
|
|
|
self.assert_image_equal(im, frame1.convert("RGBA"))
|
|
|
|
|
|
|
|
# Compare second frame to original
|
|
|
|
im.seek(1)
|
|
|
|
im.load()
|
|
|
|
self.assert_image_equal(im, frame2.convert("RGBA"))
|
|
|
|
|
|
|
|
with Image.open("Tests/images/anim_frame1.webp") as frame1:
|
|
|
|
with Image.open("Tests/images/anim_frame2.webp") as frame2:
|
|
|
|
temp_file1 = self.tempfile("temp.webp")
|
|
|
|
frame1.copy().save(
|
|
|
|
temp_file1, save_all=True, append_images=[frame2], lossless=True
|
|
|
|
)
|
|
|
|
check(temp_file1)
|
|
|
|
|
|
|
|
# Tests appending using a generator
|
|
|
|
def imGenerator(ims):
|
|
|
|
yield from ims
|
|
|
|
|
|
|
|
temp_file2 = self.tempfile("temp_generator.webp")
|
|
|
|
frame1.copy().save(
|
|
|
|
temp_file2,
|
|
|
|
save_all=True,
|
|
|
|
append_images=imGenerator([frame2]),
|
|
|
|
lossless=True,
|
|
|
|
)
|
|
|
|
check(temp_file2)
|
2017-09-28 07:22:05 +03:00
|
|
|
|
2017-09-27 06:27:40 +03:00
|
|
|
def test_timestamp_and_duration(self):
|
|
|
|
"""
|
|
|
|
Try passing a list of durations, and make sure the encoded
|
|
|
|
timestamps and durations are correct.
|
|
|
|
"""
|
|
|
|
|
|
|
|
durations = [0, 10, 20, 30, 40]
|
|
|
|
temp_file = self.tempfile("temp.webp")
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/anim_frame1.webp") as frame1:
|
|
|
|
with Image.open("Tests/images/anim_frame2.webp") as frame2:
|
|
|
|
frame1.save(
|
|
|
|
temp_file,
|
|
|
|
save_all=True,
|
|
|
|
append_images=[frame2, frame1, frame2, frame1],
|
|
|
|
duration=durations,
|
|
|
|
)
|
|
|
|
|
|
|
|
with Image.open(temp_file) as im:
|
|
|
|
self.assertEqual(im.n_frames, 5)
|
|
|
|
self.assertTrue(im.is_animated)
|
|
|
|
|
|
|
|
# Check that timestamps and durations match original values specified
|
|
|
|
ts = 0
|
|
|
|
for frame in range(im.n_frames):
|
|
|
|
im.seek(frame)
|
|
|
|
im.load()
|
|
|
|
self.assertEqual(im.info["duration"], durations[frame])
|
|
|
|
self.assertEqual(im.info["timestamp"], ts)
|
|
|
|
ts += durations[frame]
|
2017-09-27 06:27:40 +03:00
|
|
|
|
|
|
|
def test_seeking(self):
|
|
|
|
"""
|
2017-10-02 01:23:18 +03:00
|
|
|
Create an animated WebP file, and then try seeking through
|
2017-09-27 06:27:40 +03:00
|
|
|
frames in reverse-order, verifying the timestamps and durations
|
|
|
|
are correct.
|
|
|
|
"""
|
|
|
|
|
|
|
|
dur = 33
|
|
|
|
temp_file = self.tempfile("temp.webp")
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/anim_frame1.webp") as frame1:
|
|
|
|
with Image.open("Tests/images/anim_frame2.webp") as frame2:
|
|
|
|
frame1.save(
|
|
|
|
temp_file,
|
|
|
|
save_all=True,
|
|
|
|
append_images=[frame2, frame1, frame2, frame1],
|
|
|
|
duration=dur,
|
|
|
|
)
|
|
|
|
|
|
|
|
with Image.open(temp_file) as im:
|
|
|
|
self.assertEqual(im.n_frames, 5)
|
|
|
|
self.assertTrue(im.is_animated)
|
|
|
|
|
|
|
|
# Traverse frames in reverse, checking timestamps and durations
|
|
|
|
ts = dur * (im.n_frames - 1)
|
|
|
|
for frame in reversed(range(im.n_frames)):
|
|
|
|
im.seek(frame)
|
|
|
|
im.load()
|
|
|
|
self.assertEqual(im.info["duration"], dur)
|
|
|
|
self.assertEqual(im.info["timestamp"], ts)
|
|
|
|
ts -= dur
|