Since Plone 5.1 - PNG 24bit, small image scales rendered as 8-bit palette

I just came across a weird imagescaling artifact after upgrading a Plone 5.0 instance to 5.1 (don't ask).

What I noticed first was that some "thumb 128:128" scales looked dithered and some did not. I compared the Pillow versions, re-uploaded the original images and saw that the scale had been regenerated, tried saving different PNG variations of the original - all with the same result.

In the meantime, I noticed that the same image at a higher scale would be 24 bit. I fiddled with the scale settings in the control panel. My conclusion is that depending of the scale size, a threshold is reached and a choice for palette or 24 bit is made somewhere. This varies per image.

Here two scales of the same original: respectively 293 and 294 pixels wide. Where should I look to remove this over-optimization?

51207484-f20f-4252-b914-6efdacf567a0-293-palette
ed8c6794-68d8-4b94-b471-98d0c7218e33-294-24bit

While at it, I will also check what happens on the latest Plone 6 and report back...

Edit... well that's just great!

Version Overview

Plone 6.0.7 (6018)
CMF 3.2
Zope 5.8.5
Python 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110]
PIL 9.5.0 (Pillow)
WSGI: On
Server: waitress 2.1.2

Screenshot from 2023-11-20 17-07-28

We have also been hit by this in Plone 6.

We patched the scale generation not to use the WEB palette but the ADAPTIVE palette. See the patch here:

1 Like

Thanks @erral

My patch doesn't seem to take, strange.

I registered it with:

  <!-- 20231121 - fix scaled image dithering -->
  <monkey:patch description="Fix scaled image dithering"
    module="plone.scale.scale"
    original="scaleImage"
    replacement=".patches.plone_scale_scale.scaleImage"
    />

Edit: when I write the patch in the original, the affected imaged do not render at all. Which is good because it gives me a direction to search further...

I suspect that this issue concerns only PNG images that were saved with some specific settings. I looked at the two types of images in GIMP and with command line tools - Could not discover a difference.

Update: I was able to patch plone.scale scale.py by using the method described here:

This required the __init__ module of my package to include:

from plone.scale import scale
from .patches.plone_scale_scale import own_scaleImage

scale.scaleImage = own_scaleImage 

It was apparently not sufficient to have this in the patch itself, as was done in the eea package.

Now that I have the scaling working again, I would like to nuke the old scales. The method I used to regenerate scales while testing was to change the sizes of the "thumb" scale in the control panel. This approach on the production site results in re-using the cached scale.

What seems to work is to re-save the image, but this is something I'd rather avoid doing. I "think" I remember having seen a control panel view that takes care of clearing cached scales, but maybe I dreamt it :wink:

Can anyone help?

The scale information is saved in an annotation in the context object.

from zope.site.hooks import setSite
from zope.annotation.interfaces import IAnnotations
setSite(app.Plone)
item = app.Plone.eu.albisteak['ipuin-kontaketa-musikatua']
annotated = IAnnotations(item)
annotated['plone.scale']
...

so you can purge the scales just deleting the annotation.