Pillow/Tests/test_file_webp.py

69 lines
1.9 KiB
Python
Raw Normal View History

2013-03-19 22:43:45 +04:00
from tester import *
from PIL import Image
2014-01-21 01:05:30 +04:00
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():
assert_no_exception(lambda: _webp.WebPDecoderVersion())
2014-01-21 01:05:30 +04:00
assert_no_exception(lambda: _webp.WebPDecoderBuggyAlpha())
2013-03-19 22:43:45 +04:00
def test_read_rgb():
file_path = "Images/lena.webp"
image = Image.open(file_path)
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-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')
assert_image_similar(image, target, 20.0)
2013-03-20 00:40:10 +04:00
def test_write_rgb():
"""
Can we write a RGB mode file to webp without error. Does it have the bits we
expect?
"""
temp_file = tempfile("temp.webp")
lena("RGB").save(temp_file)
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())
# 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-03-20 00:49:01 +04:00
# generated with: dwebp -ppm temp.webp -o lena_webp_write.ppm
#target = Image.open('Tests/images/lena_webp_write.ppm')
#assert_image_equal(image, target)
# 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")
assert_image_similar(image, target, 20.0)