Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: avoid to convert webp to jpeg #85

Merged
merged 6 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/85.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
avoid to convert webp to jpeg @mamico
mamico marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 4 additions & 4 deletions plone/scale/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down Expand Up @@ -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,
Expand Down
Binary file added plone/scale/tests/data/profile.webp
Binary file not shown.
11 changes: 11 additions & 0 deletions plone/scale/tests/test_scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down