diff --git a/news/85.feature b/news/85.feature new file mode 100644 index 0000000..fc77f79 --- /dev/null +++ b/news/85.feature @@ -0,0 +1 @@ +Keep scaled WEBP images in WEBP format instead of converting to JPEG. @mamico diff --git a/plone/scale/scale.py b/plone/scale/scale.py index 77f2592..01ba778 100644 --- a/plone/scale/scale.py +++ b/plone/scale/scale.py @@ -67,8 +67,8 @@ def scaleImage( The `width`, `height`, `mode` parameters will be passed to :meth:`scalePILImage`, which performs the actual scaling. - The generated image is a JPEG image, unless the original is a PNG or GIF - image. This is needed to make sure alpha channel information is + The generated image is a JPEG image, unless the original is a WEBP, PNG + or GIF image. This is needed to make sure alpha channel information is not lost, which JPEG does not support. """ if isinstance(image, (bytes, str)): @@ -112,8 +112,8 @@ def scaleImage( else: # All other formats only process a single frame - if format_ not in ("PNG", "GIF"): - # Always generate JPEG, except if format is PNG or GIF. + if format_ not in ("PNG", "GIF", "WEBP"): + # Always generate JPEG, except if format is WEBP, PNG or GIF. format_ = "JPEG" image, format_ = scaleSingleFrame( img, diff --git a/plone/scale/tests/data/profile.webp b/plone/scale/tests/data/profile.webp new file mode 100644 index 0000000..2d0b9b0 Binary files /dev/null and b/plone/scale/tests/data/profile.webp differ diff --git a/plone/scale/tests/test_scale.py b/plone/scale/tests/test_scale.py index 1f75d48..58baed9 100644 --- a/plone/scale/tests/test_scale.py +++ b/plone/scale/tests/test_scale.py @@ -22,6 +22,8 @@ CMYK = fio.read() with open(os.path.join(TEST_DATA_LOCATION, "profile.jpg"), "rb") as fio: PROFILE = fio.read() +with open(os.path.join(TEST_DATA_LOCATION, "profile.webp"), "rb") as fio: + PROFILE_WEBP = fio.read() with open(os.path.join(TEST_DATA_LOCATION, "animated.gif"), "rb") as fio: ANIGIF = fio.read() with open(os.path.join(TEST_DATA_LOCATION, "animated2.gif"), "rb") as fio: @@ -90,6 +92,15 @@ def testScaleWithFewColorsStaysColored(self): self.assertEqual(image.mode, "RGB") self.assertEqual(image.format, "JPEG") + def testScaledWebp(self): + (imagedata, format, size) = scaleImage(PROFILE_WEBP, 120, 120) + self.assertEqual(format, "WEBP") + self.assertEqual(size, (120, 120)) + self.assertTrue(len(imagedata) < len(PROFILE_WEBP)) + input = StringIO(imagedata) + image = PIL.Image.open(input) + self.assertIsNotNone(image.info.get("icc_profile")) + def testAutomaticGreyscale(self): src = PIL.Image.new("RGB", (256, 256), (255, 255, 255)) draw = PIL.ImageDraw.Draw(src)