2019-01-13 20:00:12 +03:00
|
|
|
from .helper import unittest, PillowTestCase, hopper
|
2013-03-19 22:43:45 +04:00
|
|
|
|
2018-09-30 08:34:27 +03:00
|
|
|
from PIL import Image, WebPImagePlugin
|
2013-03-19 22:43:45 +04:00
|
|
|
|
2013-03-27 21:00:35 +04:00
|
|
|
try:
|
2013-04-09 08:53:59 +04:00
|
|
|
from PIL import _webp
|
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
|
|
|
|
2018-10-18 10:32:17 +03:00
|
|
|
class TestUnsupportedWebp(PillowTestCase):
|
2018-09-30 08:34:27 +03:00
|
|
|
def test_unsupported(self):
|
|
|
|
if HAVE_WEBP:
|
|
|
|
WebPImagePlugin.SUPPORTED = False
|
|
|
|
|
|
|
|
file_path = "Tests/images/hopper.webp"
|
2018-10-24 19:05:54 +03:00
|
|
|
self.assert_warning(
|
|
|
|
UserWarning,
|
|
|
|
lambda: self.assertRaises(IOError, Image.open, file_path)
|
|
|
|
)
|
2018-09-30 08:34:27 +03:00
|
|
|
|
|
|
|
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()
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_read_rgb(self):
|
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
|
|
|
|
2018-08-04 00:21:47 +03:00
|
|
|
image = Image.open("Tests/images/hopper.webp")
|
2013-03-20 00:40:10 +04:00
|
|
|
|
2017-09-28 05:28:43 +03:00
|
|
|
self.assertEqual(image.mode, self.rgb_mode)
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(image.size, (128, 128))
|
|
|
|
self.assertEqual(image.format, "WEBP")
|
|
|
|
image.load()
|
|
|
|
image.getdata()
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# generated with:
|
2014-09-22 20:00:22 +04:00
|
|
|
# dwebp -ppm ../../Tests/images/hopper.webp -o hopper_webp_bits.ppm
|
2018-09-27 13:35:00 +03:00
|
|
|
self.assert_image_similar_tofile(
|
|
|
|
image, 'Tests/images/hopper_webp_bits.ppm', 1.0)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
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?
|
|
|
|
"""
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
temp_file = self.tempfile("temp.webp")
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2017-09-28 05:28:43 +03:00
|
|
|
hopper(self.rgb_mode).save(temp_file)
|
2014-06-10 13:10:47 +04:00
|
|
|
image = Image.open(temp_file)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2017-09-28 05:28:43 +03:00
|
|
|
self.assertEqual(image.mode, self.rgb_mode)
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(image.size, (128, 128))
|
|
|
|
self.assertEqual(image.format, "WEBP")
|
|
|
|
image.load()
|
|
|
|
image.getdata()
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2014-09-22 20:00:22 +04:00
|
|
|
# generated with: dwebp -ppm temp.webp -o hopper_webp_write.ppm
|
2018-09-27 13:35:00 +03:00
|
|
|
self.assert_image_similar_tofile(
|
|
|
|
image, 'Tests/images/hopper_webp_write.ppm', 12.0)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2014-06-10 13:10:47 +04: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
|
2014-09-22 20:00:22 +04:00
|
|
|
# the image. The old lena images for WebP are showing ~16 on
|
2014-06-10 13:10:47 +04:00
|
|
|
# Ubuntu, the jpegs are showing ~18.
|
2017-09-28 05:28:43 +03:00
|
|
|
target = hopper(self.rgb_mode)
|
2017-09-27 06:27:40 +03:00
|
|
|
self.assert_image_similar(image, target, 12.0)
|
|
|
|
|
|
|
|
def test_write_unsupported_mode_L(self):
|
|
|
|
"""
|
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
|
|
|
|
2015-07-03 08:03:25 +03:00
|
|
|
temp_file = self.tempfile("temp.webp")
|
2017-09-27 06:27:40 +03:00
|
|
|
hopper("L").save(temp_file)
|
|
|
|
image = Image.open(temp_file)
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2017-09-28 05:28:43 +03:00
|
|
|
self.assertEqual(image.mode, self.rgb_mode)
|
2017-09-27 06:27:40 +03:00
|
|
|
self.assertEqual(image.size, (128, 128))
|
|
|
|
self.assertEqual(image.format, "WEBP")
|
|
|
|
|
|
|
|
image.load()
|
|
|
|
image.getdata()
|
2017-09-28 05:28:43 +03:00
|
|
|
target = hopper("L").convert(self.rgb_mode)
|
2017-09-27 06:27:40 +03:00
|
|
|
|
|
|
|
self.assert_image_similar(image, target, 10.0)
|
|
|
|
|
|
|
|
def test_write_unsupported_mode_P(self):
|
|
|
|
"""
|
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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
temp_file = self.tempfile("temp.webp")
|
|
|
|
hopper("P").save(temp_file)
|
|
|
|
image = Image.open(temp_file)
|
|
|
|
|
2017-09-28 05:28:43 +03:00
|
|
|
self.assertEqual(image.mode, self.rgb_mode)
|
2017-09-27 06:27:40 +03:00
|
|
|
self.assertEqual(image.size, (128, 128))
|
|
|
|
self.assertEqual(image.format, "WEBP")
|
|
|
|
|
|
|
|
image.load()
|
|
|
|
image.getdata()
|
2017-09-28 05:28:43 +03:00
|
|
|
target = hopper("P").convert(self.rgb_mode)
|
2017-09-27 06:27:40 +03:00
|
|
|
|
|
|
|
self.assert_image_similar(image, target, 50.0)
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2017-05-12 18:11:58 +03:00
|
|
|
def test_WebPEncode_with_invalid_args(self):
|
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:
|
2017-09-27 06:27:40 +03:00
|
|
|
self.assertRaises(TypeError, _webp.WebPAnimEncoder)
|
2017-05-12 18:11:58 +03:00
|
|
|
self.assertRaises(TypeError, _webp.WebPEncode)
|
|
|
|
|
|
|
|
def test_WebPDecode_with_invalid_args(self):
|
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:
|
2017-09-27 06:27:40 +03:00
|
|
|
self.assertRaises(TypeError, _webp.WebPAnimDecoder)
|
2017-05-12 18:11:58 +03:00
|
|
|
self.assertRaises(TypeError, _webp.WebPDecode)
|
2013-10-22 23:32:31 +04:00
|
|
|
|
2018-09-08 16:08:17 +03:00
|
|
|
def test_no_resource_warning(self):
|
|
|
|
file_path = "Tests/images/hopper.webp"
|
|
|
|
image = Image.open(file_path)
|
|
|
|
|
|
|
|
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"
|
|
|
|
with open(file_path, 'rb') as blob:
|
|
|
|
Image.open(blob).load()
|
|
|
|
Image.open(blob).load()
|
|
|
|
|
2018-11-20 11:50:14 +03:00
|
|
|
@unittest.skipUnless(HAVE_WEBP and _webp.HAVE_WEBPANIM,
|
|
|
|
"WebP save all not available")
|
|
|
|
def test_background_from_gif(self):
|
|
|
|
im = Image.open("Tests/images/chi.gif")
|
|
|
|
original_value = im.convert("RGB").getpixel((1, 1))
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
reread = Image.open(out_gif)
|
|
|
|
reread_value = reread.convert("RGB").getpixel((1, 1))
|
|
|
|
difference = sum([abs(original_value[i] - reread_value[i])
|
|
|
|
for i in range(0, 3)])
|
|
|
|
self.assertLess(difference, 5)
|
|
|
|
|
2017-09-27 06:27:40 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|