2019-11-21 05:42:52 +03:00
|
|
|
import unittest
|
|
|
|
|
2014-01-21 01:05:30 +04:00
|
|
|
from PIL import Image
|
|
|
|
|
2019-11-21 05:42:52 +03:00
|
|
|
from .helper import PillowTestCase, hopper
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2014-01-21 01:05:30 +04:00
|
|
|
try:
|
|
|
|
from PIL import _webp
|
2014-10-01 17:50:33 +04:00
|
|
|
except ImportError:
|
2018-08-04 20:46:03 +03:00
|
|
|
_webp = None
|
2014-01-21 01:05:30 +04:00
|
|
|
|
|
|
|
|
2018-08-04 20:46:03 +03:00
|
|
|
@unittest.skipIf(_webp is None, "WebP support not installed")
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestFileWebpAlpha(PillowTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
if _webp.WebPDecoderBuggyAlpha(self):
|
2019-06-13 18:54:11 +03:00
|
|
|
self.skipTest(
|
|
|
|
"Buggy early version of WebP installed, not testing transparency"
|
|
|
|
)
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_read_rgba(self):
|
2017-09-27 06:27:40 +03:00
|
|
|
"""
|
2017-10-02 01:23:18 +03:00
|
|
|
Can we read an RGBA mode file without error?
|
2017-09-27 06:27:40 +03:00
|
|
|
Does it have the bits we expect?
|
|
|
|
"""
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Generated with `cwebp transparent.png -o transparent.webp`
|
2014-06-23 10:19:29 +04:00
|
|
|
file_path = "Tests/images/transparent.webp"
|
2014-06-10 13:10:47 +04:00
|
|
|
image = Image.open(file_path)
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(image.mode, "RGBA")
|
|
|
|
self.assertEqual(image.size, (200, 150))
|
|
|
|
self.assertEqual(image.format, "WEBP")
|
|
|
|
image.load()
|
|
|
|
image.getdata()
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
image.tobytes()
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
target = Image.open("Tests/images/transparent.png")
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assert_image_similar(image, target, 20.0)
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_write_lossless_rgb(self):
|
2017-09-27 06:27:40 +03:00
|
|
|
"""
|
2017-10-02 01:23:18 +03:00
|
|
|
Can we write an RGBA mode file with lossless compression without
|
|
|
|
error? Does it have the bits we expect?
|
2017-09-27 06:27:40 +03:00
|
|
|
"""
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
temp_file = self.tempfile("temp.webp")
|
|
|
|
# temp_file = "temp.webp"
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
pil_image = hopper("RGBA")
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
mask = Image.new("RGBA", (64, 64), (128, 128, 128, 128))
|
|
|
|
# Add some partially transparent bits:
|
|
|
|
pil_image.paste(mask, (0, 0), mask)
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
pil_image.save(temp_file, lossless=True)
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
image = Image.open(temp_file)
|
|
|
|
image.load()
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(image.mode, "RGBA")
|
|
|
|
self.assertEqual(image.size, pil_image.size)
|
|
|
|
self.assertEqual(image.format, "WEBP")
|
|
|
|
image.load()
|
|
|
|
image.getdata()
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assert_image_equal(image, pil_image)
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_write_rgba(self):
|
|
|
|
"""
|
|
|
|
Can we write a RGBA mode file to webp without error.
|
|
|
|
Does it have the bits we expect?
|
|
|
|
"""
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
temp_file = self.tempfile("temp.webp")
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
pil_image = Image.new("RGBA", (10, 10), (255, 0, 0, 20))
|
|
|
|
pil_image.save(temp_file)
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if _webp.WebPDecoderBuggyAlpha(self):
|
|
|
|
return
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
image = Image.open(temp_file)
|
|
|
|
image.load()
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(image.mode, "RGBA")
|
|
|
|
self.assertEqual(image.size, (10, 10))
|
|
|
|
self.assertEqual(image.format, "WEBP")
|
2014-10-01 17:50:33 +04:00
|
|
|
image.load()
|
|
|
|
image.getdata()
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2015-07-03 09:22:56 +03:00
|
|
|
# early versions of webp are known to produce higher deviations:
|
|
|
|
# deal with it
|
2015-02-06 11:04:23 +03:00
|
|
|
if _webp.WebPDecoderVersion(self) <= 0x201:
|
|
|
|
self.assert_image_similar(image, pil_image, 3.0)
|
|
|
|
else:
|
|
|
|
self.assert_image_similar(image, pil_image, 1.0)
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2017-09-27 06:27:40 +03:00
|
|
|
def test_write_unsupported_mode_PA(self):
|
|
|
|
"""
|
2017-10-02 01:23:18 +03:00
|
|
|
Saving a palette-based file with transparency to WebP format
|
2017-09-27 06:27:40 +03:00
|
|
|
should work, and be similar to the original file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
temp_file = self.tempfile("temp.webp")
|
|
|
|
file_path = "Tests/images/transparent.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(file_path) as im:
|
|
|
|
im.save(temp_file)
|
2017-09-27 06:27:40 +03:00
|
|
|
image = Image.open(temp_file)
|
|
|
|
|
|
|
|
self.assertEqual(image.mode, "RGBA")
|
|
|
|
self.assertEqual(image.size, (200, 150))
|
|
|
|
self.assertEqual(image.format, "WEBP")
|
|
|
|
|
|
|
|
image.load()
|
|
|
|
image.getdata()
|
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(file_path) as im:
|
|
|
|
target = im.convert("RGBA")
|
2017-09-27 06:27:40 +03:00
|
|
|
|
2017-12-06 16:46:56 +03:00
|
|
|
self.assert_image_similar(image, target, 25.0)
|