From 80b96246c4d0f27221b2cac0b2c0fc8a025de430 Mon Sep 17 00:00:00 2001 From: Jason Douglas Date: Wed, 27 Sep 2017 19:28:43 -0700 Subject: [PATCH] Fix tests to support different output modes (RGB vs RGBX) --- PIL/WebPImagePlugin.py | 7 ++++++- Tests/test_file_webp.py | 31 ++++++++++++++++--------------- Tests/test_file_webp_animated.py | 10 ++++------ Tests/test_file_webp_lossless.py | 19 ++++++++++--------- Tests/test_file_webp_metadata.py | 6 ++---- 5 files changed, 38 insertions(+), 35 deletions(-) diff --git a/PIL/WebPImagePlugin.py b/PIL/WebPImagePlugin.py index 9b15bf14f..f23c793f8 100644 --- a/PIL/WebPImagePlugin.py +++ b/PIL/WebPImagePlugin.py @@ -35,7 +35,12 @@ class WebPImageFile(ImageFile.ImageFile): def _open(self): if not _webp.HAVE_WEBPANIM: # 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.fp = BytesIO(data) self.tile = [("raw", (0, 0) + self.size, 0, self.mode)] diff --git a/Tests/test_file_webp.py b/Tests/test_file_webp.py index 63ef06b9a..1ae75d573 100644 --- a/Tests/test_file_webp.py +++ b/Tests/test_file_webp.py @@ -4,18 +4,19 @@ from PIL import Image try: from PIL import _webp + HAVE_WEBP = True except ImportError: - # Skip in setUp() - pass - + HAVE_WEBP = False class TestFileWebp(PillowTestCase): def setUp(self): - try: - from PIL import _webp - except ImportError: + if not HAVE_WEBP: 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): _webp.WebPDecoderVersion() @@ -30,7 +31,7 @@ class TestFileWebp(PillowTestCase): file_path = "Tests/images/hopper.webp" 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.format, "WEBP") image.load() @@ -38,7 +39,7 @@ class TestFileWebp(PillowTestCase): # generated with: # 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) def test_write_rgb(self): @@ -49,10 +50,10 @@ class TestFileWebp(PillowTestCase): temp_file = self.tempfile("temp.webp") - hopper("RGBX").save(temp_file) + hopper(self.rgb_mode).save(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.format, "WEBP") image.load() @@ -71,7 +72,7 @@ class TestFileWebp(PillowTestCase): # 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 # Ubuntu, the jpegs are showing ~18. - target = hopper("RGBX") + target = hopper(self.rgb_mode) self.assert_image_similar(image, target, 12.0) def test_write_unsupported_mode_L(self): @@ -84,13 +85,13 @@ class TestFileWebp(PillowTestCase): hopper("L").save(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.format, "WEBP") image.load() image.getdata() - target = hopper("L").convert("RGBX") + target = hopper("L").convert(self.rgb_mode) self.assert_image_similar(image, target, 10.0) @@ -104,13 +105,13 @@ class TestFileWebp(PillowTestCase): hopper("P").save(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.format, "WEBP") image.load() image.getdata() - target = hopper("P").convert("RGBX") + target = hopper("P").convert(self.rgb_mode) self.assert_image_similar(image, target, 50.0) diff --git a/Tests/test_file_webp_animated.py b/Tests/test_file_webp_animated.py index 16c6de129..02cf5a146 100644 --- a/Tests/test_file_webp_animated.py +++ b/Tests/test_file_webp_animated.py @@ -4,18 +4,16 @@ from PIL import Image try: from PIL import _webp + HAVE_WEBP = True except ImportError: - pass - # Skip in setUp() - + HAVE_WEBP = False class TestFileWebpAnimation(PillowTestCase): def setUp(self): - try: - from PIL import _webp - except ImportError: + if not HAVE_WEBP: self.skipTest('WebP support not installed') + return if not _webp.HAVE_WEBPANIM: self.skipTest("WebP library does not contain animation support, " diff --git a/Tests/test_file_webp_lossless.py b/Tests/test_file_webp_lossless.py index d0a94cb39..dfcbd869a 100644 --- a/Tests/test_file_webp_lossless.py +++ b/Tests/test_file_webp_lossless.py @@ -4,37 +4,38 @@ from PIL import Image try: from PIL import _webp + HAVE_WEBP = True except ImportError: - pass - # Skip in setUp() - + HAVE_WEBP = False class TestFileWebpLossless(PillowTestCase): def setUp(self): - try: - from PIL import _webp - except: + if not HAVE_WEBP: self.skipTest('WebP support not installed') + return if (_webp.WebPDecoderVersion() < 0x0200): 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): 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.load() - self.assertEqual(image.mode, "RGBX") + self.assertEqual(image.mode, self.rgb_mode) self.assertEqual(image.size, (128, 128)) self.assertEqual(image.format, "WEBP") image.load() image.getdata() - self.assert_image_equal(image, hopper("RGBX")) + self.assert_image_equal(image, hopper(self.rgb_mode)) if __name__ == '__main__': diff --git a/Tests/test_file_webp_metadata.py b/Tests/test_file_webp_metadata.py index cb45f45fa..805125357 100644 --- a/Tests/test_file_webp_metadata.py +++ b/Tests/test_file_webp_metadata.py @@ -5,8 +5,6 @@ from PIL import Image try: from PIL import _webp HAVE_WEBP = True - HAVE_WEBPMUX = _webp.HAVE_WEBPMUX - HAVE_WEBPANIM = _webp.HAVE_WEBPANIM except ImportError: HAVE_WEBP = False @@ -17,7 +15,7 @@ class TestFileWebpMetadata(PillowTestCase): self.skipTest('WebP support not installed') return - if not HAVE_WEBPMUX: + if not _webp.HAVE_WEBPMUX: self.skipTest('WebPMux support not installed') def test_read_exif_metadata(self): @@ -112,7 +110,7 @@ class TestFileWebpMetadata(PillowTestCase): 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): iccp_data = ''.encode('utf-8') exif_data = ''.encode('utf-8')