2014-07-07 21:03:50 +04:00
|
|
|
from helper import unittest, PillowTestCase, lena
|
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
|
2013-03-27 21:00:35 +04:00
|
|
|
except:
|
2014-06-10 13:10:47 +04:00
|
|
|
# Skip in setUp()
|
|
|
|
pass
|
2013-03-19 22:43:45 +04:00
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestFileWebp(PillowTestCase):
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def setUp(self):
|
|
|
|
try:
|
|
|
|
from PIL import _webp
|
|
|
|
except:
|
|
|
|
self.skipTest('WebP support not installed')
|
2013-07-01 02:42:19 +04:00
|
|
|
|
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):
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2014-06-23 10:19:29 +04:00
|
|
|
file_path = "Tests/images/lena.webp"
|
2014-06-10 13:10:47 +04:00
|
|
|
image = Image.open(file_path)
|
2013-03-20 00:40:10 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(image.mode, "RGB")
|
|
|
|
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-06-23 10:19:29 +04:00
|
|
|
# dwebp -ppm ../../Tests/images/lena.webp -o lena_webp_bits.ppm
|
2014-06-10 13:10:47 +04:00
|
|
|
target = Image.open('Tests/images/lena_webp_bits.ppm')
|
|
|
|
self.assert_image_similar(image, target, 20.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
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
lena("RGB").save(temp_file)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
image = Image.open(temp_file)
|
|
|
|
image.load()
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(image.mode, "RGB")
|
|
|
|
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
|
|
|
# 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.
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# generated with: dwebp -ppm temp.webp -o lena_webp_write.ppm
|
|
|
|
# target = Image.open('Tests/images/lena_webp_write.ppm')
|
|
|
|
# self.assert_image_equal(image, target)
|
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
|
|
|
|
# the image. The included lena images for WebP are showing ~16 on
|
|
|
|
# Ubuntu, the jpegs are showing ~18.
|
|
|
|
target = lena("RGB")
|
|
|
|
self.assert_image_similar(image, target, 20.0)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-10-22 23:32:31 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
2013-10-22 23:32:31 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# End of file
|