Fix tests to support different output modes (RGB vs RGBX)

This commit is contained in:
Jason Douglas 2017-09-27 19:28:43 -07:00
parent c18d26b04b
commit 80b96246c4
5 changed files with 38 additions and 35 deletions

View File

@ -35,7 +35,12 @@ class WebPImageFile(ImageFile.ImageFile):
def _open(self): def _open(self):
if not _webp.HAVE_WEBPANIM: if not _webp.HAVE_WEBPANIM:
# Legacy mode # Legacy mode
data, width, height, self.mode = _webp.WebPDecode(self.fp.read()) data, width, height, self.mode, icc_profile, exif = \
_webp.WebPDecode(self.fp.read())
if icc_profile:
self.info["icc_profile"] = icc_profile
if exif:
self.info["exif"] = exif
self.size = width, height self.size = width, height
self.fp = BytesIO(data) self.fp = BytesIO(data)
self.tile = [("raw", (0, 0) + self.size, 0, self.mode)] self.tile = [("raw", (0, 0) + self.size, 0, self.mode)]

View File

@ -4,18 +4,19 @@ from PIL import Image
try: try:
from PIL import _webp from PIL import _webp
HAVE_WEBP = True
except ImportError: except ImportError:
# Skip in setUp() HAVE_WEBP = False
pass
class TestFileWebp(PillowTestCase): class TestFileWebp(PillowTestCase):
def setUp(self): def setUp(self):
try: if not HAVE_WEBP:
from PIL import _webp
except ImportError:
self.skipTest('WebP support not installed') self.skipTest('WebP support not installed')
return
# WebPAnimDecoder only retuns RGBA or RGBX, never RGB
self.rgb_mode = "RGBX" if _webp.HAVE_WEBPANIM else "RGB"
def test_version(self): def test_version(self):
_webp.WebPDecoderVersion() _webp.WebPDecoderVersion()
@ -30,7 +31,7 @@ class TestFileWebp(PillowTestCase):
file_path = "Tests/images/hopper.webp" file_path = "Tests/images/hopper.webp"
image = Image.open(file_path) image = Image.open(file_path)
self.assertEqual(image.mode, "RGBX") self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128)) self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP") self.assertEqual(image.format, "WEBP")
image.load() image.load()
@ -38,7 +39,7 @@ class TestFileWebp(PillowTestCase):
# generated with: # generated with:
# dwebp -ppm ../../Tests/images/hopper.webp -o hopper_webp_bits.ppm # dwebp -ppm ../../Tests/images/hopper.webp -o hopper_webp_bits.ppm
target = Image.open('Tests/images/hopper_webp_bits.ppm').convert("RGBX") target = Image.open('Tests/images/hopper_webp_bits.ppm').convert(self.rgb_mode)
self.assert_image_similar(image, target, 20.0) self.assert_image_similar(image, target, 20.0)
def test_write_rgb(self): def test_write_rgb(self):
@ -49,10 +50,10 @@ class TestFileWebp(PillowTestCase):
temp_file = self.tempfile("temp.webp") temp_file = self.tempfile("temp.webp")
hopper("RGBX").save(temp_file) hopper(self.rgb_mode).save(temp_file)
image = Image.open(temp_file) image = Image.open(temp_file)
self.assertEqual(image.mode, "RGBX") self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128)) self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP") self.assertEqual(image.format, "WEBP")
image.load() image.load()
@ -71,7 +72,7 @@ class TestFileWebp(PillowTestCase):
# then we're going to accept that it's a reasonable lossy version of # then we're going to accept that it's a reasonable lossy version of
# the image. The old lena images for WebP are showing ~16 on # the image. The old lena images for WebP are showing ~16 on
# Ubuntu, the jpegs are showing ~18. # Ubuntu, the jpegs are showing ~18.
target = hopper("RGBX") target = hopper(self.rgb_mode)
self.assert_image_similar(image, target, 12.0) self.assert_image_similar(image, target, 12.0)
def test_write_unsupported_mode_L(self): def test_write_unsupported_mode_L(self):
@ -84,13 +85,13 @@ class TestFileWebp(PillowTestCase):
hopper("L").save(temp_file) hopper("L").save(temp_file)
image = Image.open(temp_file) image = Image.open(temp_file)
self.assertEqual(image.mode, "RGBX") self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128)) self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP") self.assertEqual(image.format, "WEBP")
image.load() image.load()
image.getdata() image.getdata()
target = hopper("L").convert("RGBX") target = hopper("L").convert(self.rgb_mode)
self.assert_image_similar(image, target, 10.0) self.assert_image_similar(image, target, 10.0)
@ -104,13 +105,13 @@ class TestFileWebp(PillowTestCase):
hopper("P").save(temp_file) hopper("P").save(temp_file)
image = Image.open(temp_file) image = Image.open(temp_file)
self.assertEqual(image.mode, "RGBX") self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128)) self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP") self.assertEqual(image.format, "WEBP")
image.load() image.load()
image.getdata() image.getdata()
target = hopper("P").convert("RGBX") target = hopper("P").convert(self.rgb_mode)
self.assert_image_similar(image, target, 50.0) self.assert_image_similar(image, target, 50.0)

View File

@ -4,18 +4,16 @@ from PIL import Image
try: try:
from PIL import _webp from PIL import _webp
HAVE_WEBP = True
except ImportError: except ImportError:
pass HAVE_WEBP = False
# Skip in setUp()
class TestFileWebpAnimation(PillowTestCase): class TestFileWebpAnimation(PillowTestCase):
def setUp(self): def setUp(self):
try: if not HAVE_WEBP:
from PIL import _webp
except ImportError:
self.skipTest('WebP support not installed') self.skipTest('WebP support not installed')
return
if not _webp.HAVE_WEBPANIM: if not _webp.HAVE_WEBPANIM:
self.skipTest("WebP library does not contain animation support, " self.skipTest("WebP library does not contain animation support, "

View File

@ -4,37 +4,38 @@ from PIL import Image
try: try:
from PIL import _webp from PIL import _webp
HAVE_WEBP = True
except ImportError: except ImportError:
pass HAVE_WEBP = False
# Skip in setUp()
class TestFileWebpLossless(PillowTestCase): class TestFileWebpLossless(PillowTestCase):
def setUp(self): def setUp(self):
try: if not HAVE_WEBP:
from PIL import _webp
except:
self.skipTest('WebP support not installed') self.skipTest('WebP support not installed')
return
if (_webp.WebPDecoderVersion() < 0x0200): if (_webp.WebPDecoderVersion() < 0x0200):
self.skipTest('lossless not included') self.skipTest('lossless not included')
# WebPAnimDecoder only retuns RGBA or RGBX, never RGB
self.rgb_mode = "RGBX" if _webp.HAVE_WEBPANIM else "RGB"
def test_write_lossless_rgb(self): def test_write_lossless_rgb(self):
temp_file = self.tempfile("temp.webp") temp_file = self.tempfile("temp.webp")
hopper("RGBX").save(temp_file, lossless=True) hopper(self.rgb_mode).save(temp_file, lossless=True)
image = Image.open(temp_file) image = Image.open(temp_file)
image.load() image.load()
self.assertEqual(image.mode, "RGBX") self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128)) self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP") self.assertEqual(image.format, "WEBP")
image.load() image.load()
image.getdata() image.getdata()
self.assert_image_equal(image, hopper("RGBX")) self.assert_image_equal(image, hopper(self.rgb_mode))
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -5,8 +5,6 @@ from PIL import Image
try: try:
from PIL import _webp from PIL import _webp
HAVE_WEBP = True HAVE_WEBP = True
HAVE_WEBPMUX = _webp.HAVE_WEBPMUX
HAVE_WEBPANIM = _webp.HAVE_WEBPANIM
except ImportError: except ImportError:
HAVE_WEBP = False HAVE_WEBP = False
@ -17,7 +15,7 @@ class TestFileWebpMetadata(PillowTestCase):
self.skipTest('WebP support not installed') self.skipTest('WebP support not installed')
return return
if not HAVE_WEBPMUX: if not _webp.HAVE_WEBPMUX:
self.skipTest('WebPMux support not installed') self.skipTest('WebPMux support not installed')
def test_read_exif_metadata(self): def test_read_exif_metadata(self):
@ -112,7 +110,7 @@ class TestFileWebpMetadata(PillowTestCase):
self.assertFalse(webp_image._getexif()) self.assertFalse(webp_image._getexif())
@unittest.skipUnless(HAVE_WEBPANIM, 'WebP animation support not available') @unittest.skipUnless(_webp.HAVE_WEBPANIM, 'WebP animation support not available')
def test_write_animated_metadata(self): def test_write_animated_metadata(self):
iccp_data = '<iccp_data>'.encode('utf-8') iccp_data = '<iccp_data>'.encode('utf-8')
exif_data = '<exif_data>'.encode('utf-8') exif_data = '<exif_data>'.encode('utf-8')