2013-03-19 22:43:45 +04:00
|
|
|
from tester import *
|
|
|
|
|
|
|
|
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:
|
|
|
|
skip('webp support not installed')
|
2013-03-19 22:43:45 +04:00
|
|
|
|
|
|
|
|
2013-05-14 08:28:18 +04:00
|
|
|
def test_version():
|
2013-05-14 09:21:52 +04:00
|
|
|
assert_no_exception(lambda: _webp.WebPDecoderVersion())
|
2013-05-14 08:28:18 +04:00
|
|
|
|
|
|
|
def test_good_alpha():
|
2013-05-14 09:21:52 +04:00
|
|
|
assert_equal(_webp.WebPDecoderBuggyAlpha(), 0)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-03-19 22:43:45 +04:00
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
def test_read_rgb():
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
file_path = "Images/lena.webp"
|
|
|
|
image = Image.open(file_path)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
assert_equal(image.mode, "RGB")
|
|
|
|
assert_equal(image.size, (128, 128))
|
|
|
|
assert_equal(image.format, "WEBP")
|
|
|
|
assert_no_exception(lambda: image.load())
|
|
|
|
assert_no_exception(lambda: image.getdata())
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-03-20 00:40:10 +04:00
|
|
|
# generated with: dwebp -ppm ../../Images/lena.webp -o lena_webp_bits.ppm
|
|
|
|
target = Image.open('Tests/images/lena_webp_bits.ppm')
|
2014-01-05 00:48:18 +04:00
|
|
|
assert_image_similar(image, target, 20.0)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-03-20 00:40:10 +04:00
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
def test_write_rgb():
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
"""
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
temp_file = tempfile("temp.webp")
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
lena("RGB").save(temp_file)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
image = Image.open(temp_file)
|
|
|
|
image.load()
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
assert_equal(image.mode, "RGB")
|
|
|
|
assert_equal(image.size, (128, 128))
|
|
|
|
assert_equal(image.format, "WEBP")
|
|
|
|
assert_no_exception(lambda: image.load())
|
|
|
|
assert_no_exception(lambda: image.getdata())
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-03-23 08:15:45 +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
|
|
|
|
2013-03-20 00:49:01 +04:00
|
|
|
# generated with: dwebp -ppm temp.webp -o lena_webp_write.ppm
|
2013-03-23 08:15:45 +04:00
|
|
|
#target = Image.open('Tests/images/lena_webp_write.ppm')
|
2013-05-13 20:01:42 +04:00
|
|
|
#assert_image_equal(image, target)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-03-23 08:15:45 +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
|
2013-07-01 02:42:19 +04:00
|
|
|
# for webp are showing ~16 on Ubuntu, the jpegs are showing ~18.
|
2013-05-13 20:01:42 +04:00
|
|
|
target = lena("RGB")
|
|
|
|
assert_image_similar(image, target, 20.0)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
|
|
|
|
2013-10-22 23:32:31 +04:00
|
|
|
def test_write_lossless_rgb():
|
|
|
|
temp_file = tempfile("temp.webp")
|
|
|
|
|
|
|
|
lena("RGB").save(temp_file, lossless=True)
|
|
|
|
|
|
|
|
image = Image.open(temp_file)
|
|
|
|
image.load()
|
|
|
|
|
|
|
|
assert_equal(image.mode, "RGB")
|
|
|
|
assert_equal(image.size, (128, 128))
|
|
|
|
assert_equal(image.format, "WEBP")
|
|
|
|
assert_no_exception(lambda: image.load())
|
|
|
|
assert_no_exception(lambda: image.getdata())
|
|
|
|
|
|
|
|
|
|
|
|
assert_image_equal(image, lena("RGB"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
def test_write_rgba():
|
|
|
|
"""
|
|
|
|
Can we write a RGBA mode file to webp without error. Does it have the bits we
|
|
|
|
expect?
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
"""
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
temp_file = tempfile("temp.webp")
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
pil_image = Image.new("RGBA", (10, 10), (255, 0, 0, 20))
|
|
|
|
pil_image.save(temp_file)
|
2013-05-14 09:21:52 +04:00
|
|
|
|
|
|
|
if _webp.WebPDecoderBuggyAlpha():
|
|
|
|
return
|
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
image = Image.open(temp_file)
|
|
|
|
image.load()
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
assert_equal(image.mode, "RGBA")
|
|
|
|
assert_equal(image.size, (10, 10))
|
|
|
|
assert_equal(image.format, "WEBP")
|
|
|
|
assert_no_exception(image.load)
|
|
|
|
assert_no_exception(image.getdata)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
assert_image_similar(image, pil_image, 1.0)
|
2013-03-23 08:15:45 +04:00
|
|
|
|
2013-05-14 08:28:18 +04:00
|
|
|
if _webp.WebPDecoderBuggyAlpha():
|
2013-05-14 09:21:52 +04:00
|
|
|
skip("Buggy early version of webp installed, not testing transparency")
|
2013-03-19 22:43:45 +04:00
|
|
|
|
2013-05-14 08:28:18 +04:00
|
|
|
def test_read_rgba():
|
2013-05-14 09:21:52 +04:00
|
|
|
# Generated with `cwebp transparent.png -o transparent.webp`
|
|
|
|
file_path = "Images/transparent.webp"
|
|
|
|
image = Image.open(file_path)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-14 09:21:52 +04:00
|
|
|
assert_equal(image.mode, "RGBA")
|
|
|
|
assert_equal(image.size, (200, 150))
|
|
|
|
assert_equal(image.format, "WEBP")
|
|
|
|
assert_no_exception(lambda: image.load())
|
|
|
|
assert_no_exception(lambda: image.getdata())
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-14 09:21:52 +04:00
|
|
|
orig_bytes = image.tobytes()
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-14 09:21:52 +04:00
|
|
|
target = Image.open('Images/transparent.png')
|
|
|
|
assert_image_similar(image, target, 20.0)
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-10-22 23:32:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
def test_write_lossless_rgb():
|
|
|
|
temp_file = tempfile("temp.webp")
|
|
|
|
#temp_file = "temp.webp"
|
|
|
|
|
|
|
|
pil_image = lena('RGBA')
|
|
|
|
|
|
|
|
mask = Image.new("RGBA", (64, 64), (128,128,128,128))
|
|
|
|
pil_image.paste(mask, (0,0), mask) # add some partially transparent bits.
|
|
|
|
|
|
|
|
pil_image.save(temp_file, lossless=True)
|
|
|
|
|
|
|
|
image = Image.open(temp_file)
|
|
|
|
image.load()
|
|
|
|
|
|
|
|
assert_equal(image.mode, "RGBA")
|
|
|
|
assert_equal(image.size, pil_image.size)
|
|
|
|
assert_equal(image.format, "WEBP")
|
|
|
|
assert_no_exception(lambda: image.load())
|
|
|
|
assert_no_exception(lambda: image.getdata())
|
|
|
|
|
|
|
|
|
|
|
|
assert_image_equal(image, pil_image)
|