Pillow/Tests/test_file_webp.py

175 lines
5.6 KiB
Python
Raw Normal View History

import unittest
from PIL import Image, WebPImagePlugin
2013-03-19 22:43:45 +04:00
from .helper import PillowTestCase, hopper
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
HAVE_WEBP = True
except ImportError:
HAVE_WEBP = False
2013-03-19 22:43:45 +04:00
2018-10-18 10:32:17 +03:00
class TestUnsupportedWebp(PillowTestCase):
def test_unsupported(self):
if HAVE_WEBP:
WebPImagePlugin.SUPPORTED = False
file_path = "Tests/images/hopper.webp"
self.assert_warning(
2019-06-13 18:54:11 +03:00
UserWarning, lambda: self.assertRaises(IOError, Image.open, file_path)
)
if HAVE_WEBP:
WebPImagePlugin.SUPPORTED = True
2018-10-18 10:32:17 +03:00
@unittest.skipIf(not HAVE_WEBP, "WebP support not installed")
class TestFileWebp(PillowTestCase):
def setUp(self):
self.rgb_mode = "RGB"
2014-06-10 13:10:47 +04:00
def test_version(self):
_webp.WebPDecoderVersion()
_webp.WebPDecoderBuggyAlpha()
2014-06-10 13:10:47 +04:00
def test_read_rgb(self):
"""
Can we read a RGB mode WebP file without error?
Does it have the bits we expect?
"""
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/hopper.webp") as image:
self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP")
image.load()
image.getdata()
# generated with:
# dwebp -ppm ../../Tests/images/hopper.webp -o hopper_webp_bits.ppm
self.assert_image_similar_tofile(
image, "Tests/images/hopper_webp_bits.ppm", 1.0
)
2014-06-10 13:10:47 +04:00
def test_write_rgb(self):
"""
Can we write a RGB mode file to webp without error.
Does it have the bits we expect?
"""
2014-06-10 13:10:47 +04:00
temp_file = self.tempfile("temp.webp")
hopper(self.rgb_mode).save(temp_file)
2019-11-25 23:03:23 +03:00
with Image.open(temp_file) as image:
self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP")
image.load()
image.getdata()
# generated with: dwebp -ppm temp.webp -o hopper_webp_write.ppm
self.assert_image_similar_tofile(
image, "Tests/images/hopper_webp_write.ppm", 12.0
)
# 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
# the image. The old lena images for WebP are showing ~16 on
# Ubuntu, the jpegs are showing ~18.
target = hopper(self.rgb_mode)
self.assert_image_similar(image, target, 12.0)
def test_write_unsupported_mode_L(self):
"""
Saving a black-and-white file to WebP format should work, and be
similar to the original file.
"""
2015-07-03 08:03:25 +03:00
temp_file = self.tempfile("temp.webp")
hopper("L").save(temp_file)
2019-11-25 23:03:23 +03:00
with Image.open(temp_file) as image:
self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP")
2015-07-03 08:03:25 +03:00
2019-11-25 23:03:23 +03:00
image.load()
image.getdata()
target = hopper("L").convert(self.rgb_mode)
2019-11-25 23:03:23 +03:00
self.assert_image_similar(image, target, 10.0)
def test_write_unsupported_mode_P(self):
"""
Saving a palette-based file to WebP format should work, and be
similar to the original file.
"""
temp_file = self.tempfile("temp.webp")
hopper("P").save(temp_file)
2019-11-25 23:03:23 +03:00
with Image.open(temp_file) as image:
self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP")
2019-11-25 23:03:23 +03:00
image.load()
image.getdata()
target = hopper("P").convert(self.rgb_mode)
2019-11-25 23:03:23 +03:00
self.assert_image_similar(image, target, 50.0)
2015-07-03 08:03:25 +03:00
def test_WebPEncode_with_invalid_args(self):
"""
Calling encoder functions with no arguments should result in an error.
"""
if _webp.HAVE_WEBPANIM:
self.assertRaises(TypeError, _webp.WebPAnimEncoder)
self.assertRaises(TypeError, _webp.WebPEncode)
def test_WebPDecode_with_invalid_args(self):
"""
Calling decoder functions with no arguments should result in an error.
"""
if _webp.HAVE_WEBPANIM:
self.assertRaises(TypeError, _webp.WebPAnimDecoder)
self.assertRaises(TypeError, _webp.WebPDecode)
def test_no_resource_warning(self):
file_path = "Tests/images/hopper.webp"
2019-11-25 23:03:23 +03:00
with Image.open(file_path) as image:
temp_file = self.tempfile("temp.webp")
self.assert_warning(None, image.save, temp_file)
2018-10-18 10:32:17 +03:00
def test_file_pointer_could_be_reused(self):
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()
2019-06-13 18:54:11 +03:00
@unittest.skipUnless(
HAVE_WEBP and _webp.HAVE_WEBPANIM, "WebP save all not available"
)
def test_background_from_gif(self):
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))
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
out_webp = self.tempfile("temp.webp")
im.save(out_webp, save_all=True)
# Save as GIF
out_gif = self.tempfile("temp.gif")
Image.open(out_webp).save(out_gif)
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))
2019-06-13 18:54:11 +03:00
difference = sum(
[abs(original_value[i] - reread_value[i]) for i in range(0, 3)]
)
self.assertLess(difference, 5)