Pillow/Tests/test_file_webp.py

141 lines
4.3 KiB
Python
Raw Normal View History

2014-09-22 20:00:22 +04:00
from helper import unittest, PillowTestCase, hopper
2013-03-19 22:43:45 +04:00
from PIL import Image
2013-03-27 21:00:35 +04:00
try:
2013-04-09 08:53:59 +04:00
from PIL import _webp
HAVE_WEBP = True
except ImportError:
HAVE_WEBP = False
2013-03-19 22:43:45 +04:00
2014-06-10 13:10:47 +04:00
class TestFileWebp(PillowTestCase):
2014-06-10 13:10:47 +04:00
def setUp(self):
if not HAVE_WEBP:
2014-06-10 13:10:47 +04:00
self.skipTest('WebP support not installed')
return
2017-10-03 18:05:20 +03:00
# WebPAnimDecoder only returns RGBA or RGBX, never RGB
self.rgb_mode = "RGBX" if _webp.HAVE_WEBPANIM else "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?
"""
2014-09-22 20:00:22 +04:00
file_path = "Tests/images/hopper.webp"
2014-06-10 13:10:47 +04:00
image = Image.open(file_path)
2013-03-20 00:40:10 +04: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()
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
target = Image.open('Tests/images/hopper_webp_bits.ppm')
target = target.convert(self.rgb_mode)
2014-06-10 13:10:47 +04:00
self.assert_image_similar(image, target, 20.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)
2014-06-10 13:10:47 +04:00
image = Image.open(temp_file)
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()
2014-06-10 13:10:47 +04:00
# If we're using the exact same version of WebP, this test should pass.
# but it doesn't if the WebP is generated on Ubuntu and tested on
# Fedora.
2014-09-22 20:00:22 +04:00
# generated with: dwebp -ppm temp.webp -o hopper_webp_write.ppm
# target = Image.open('Tests/images/hopper_webp_write.ppm')
2014-06-10 13:10:47 +04:00
# self.assert_image_equal(image, target)
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.
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)
image = Image.open(temp_file)
2015-07-03 08:03:25 +03:00
self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP")
image.load()
image.getdata()
target = hopper("L").convert(self.rgb_mode)
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)
image = Image.open(temp_file)
self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP")
image.load()
image.getdata()
target = hopper("P").convert(self.rgb_mode)
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)
2014-06-10 13:10:47 +04:00
if __name__ == '__main__':
unittest.main()