2014-09-05 13:36:24 +04:00
|
|
|
from helper import unittest, PillowTestCase, hopper
|
2014-01-21 01:05:30 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
try:
|
|
|
|
from PIL import _webp
|
2014-10-01 17:50:33 +04:00
|
|
|
except ImportError:
|
2014-06-10 13:10:47 +04:00
|
|
|
pass
|
|
|
|
# Skip in setUp()
|
2014-01-21 01:05:30 +04:00
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestFileWebpAlpha(PillowTestCase):
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def setUp(self):
|
|
|
|
try:
|
|
|
|
from PIL import _webp
|
2014-10-01 17:50:33 +04:00
|
|
|
except ImportError:
|
2014-06-10 13:10:47 +04:00
|
|
|
self.skipTest('WebP support not installed')
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if _webp.WebPDecoderBuggyAlpha(self):
|
2014-08-28 15:44:19 +04: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):
|
|
|
|
# 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
|
|
|
|
2014-06-23 10:19:29 +04: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):
|
|
|
|
temp_file = self.tempfile("temp.webp")
|
|
|
|
# temp_file = "temp.webp"
|
2014-01-21 01:05:30 +04:00
|
|
|
|
2014-09-05 13:36:24 +04: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-02-06 11:04:23 +03:00
|
|
|
# early versions of webp are known to produce higher deviations: deal with it
|
|
|
|
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
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
# End of file
|