Create image scales in WEBP format

We have been lately running several performance checks in our Plone sites and Lighthouse is always suggesting to serve images as WEBP, JPEG2000 or JPG XR. I have done a small test changing the plone.scale scale method parameters sent to Pillow and request WEBP images, but Pillow fails with a TypeError.

I tested this in a Plone 5.2.7 site with Pillow 6.2.2

Has some tried to create plone.scale based image scales in WEBP or some other optimized formats so the created scales are smaller in bytes?

1 Like

Do you have WEBP support in your Pillow installation?

Yes I have. But this Pillow version (6.2.2) is quite old (January 2020).

I will try with a newer version and report back.

With Pillow 6.2.2 I get the following error:

2022-06-20 17:38:01,862 ERROR [plone.namedfile.scaling:275][waitress-3] Could not scale "<plone.namedfile.file.NamedBlobImage object at 0x7f7840d41660 oid 0x4fc2c in <Connection at 7f7841a93af0>>" of 'http://localhost:8080/Plone/en/news/product-launched'
Traceback (most recent call last):
File "/home/erral/dev/project/src/plone.namedfile/plone/namedfile/scaling.py", line 269, in handle_image
result = self.create_scale(
File "/home/erral/dev/project/src/plone.namedfile/plone/namedfile/scaling.py", line 251, in create_scale
return scaleImage(
File "/home/erral/dev/project/src/plone.scale/plone/scale/scale.py", line 115, in scaleImage
image.save(
File "/home/erral/downloads/eggs/Pillow-6.2.2-py3.8-linux-x86_64.egg/PIL/Image.py", line 2084, in save
save_handler(self, fp, filename)
File "/home/erral/downloads/eggs/Pillow-6.2.2-py3.8-linux-x86_64.egg/PIL/WebPImagePlugin.py", line 337, in _save
data = _webp.WebPEncode(
TypeError: a bytes-like object is required, not 'NoneType'

If I upgrade Pillow to 9.1.1, the error is gone, but a new one comes when plone.namedfile tries to guess the file information and get its width and height.

I am testing some changes to plone.namedfile so it can guess webp files' sizes.

I have prepared the following branches with the changes to create WEBP based image scales:

plone.scale

plone.namedfile (based on branch 5.x for Plone 5.2)

plone namedfile (based on master)

I will be testing those branches in a site of ours to check whether we get any improvement.

Is this when creating scales? If so, cant you pass the width and height from (based on) the original image ?

Yes, the error was that as the code there was not handling WEBP files directly, it takes the first bytes of the image and tries to create a PIL.Image instance to get the image information.

So I added some code there to read the image size from the first bytes and it's done.

Slighly off topic:

I have only used webp in Wordpress, but there I have a fallback for those that use browsers that does not support webp. (In other words: they get jpg)