resizing paletted PNG images in WordPress #7313
Replies: 10 comments 12 replies
-
Did you preface the output image name with "PNG8:"such as PNG8:output.png? |
Beta Was this translation helpful? Give feedback.
-
Are you shrinking or enlarging the images? A simple and obvious technique is to resize the input image, then remap the pixels to the input image. This ensures that all the output colours are present in the input image. There will be no added colours, but there may be fewer. For example:
The output is paletted, with one colour fewer than the input, and is about one-third of the size in bytes. |
Beta Was this translation helpful? Give feedback.
-
You should post this to an iMagick forum. The maintainers of Imagemagick do not maintain iMagick.
|
Beta Was this translation helpful? Give feedback.
-
You could install the "magick" portable binary version of IM in your own directory. See https://imagemagick.org/script/download.php |
Beta Was this translation helpful? Give feedback.
-
I believe that alpha_deactivate and activate are IM 7 commands. You need alpha_on and off in IM 6 or 7 |
Beta Was this translation helpful? Give feedback.
-
The default filter for
The result is paletted. |
Beta Was this translation helpful? Give feedback.
-
I took your image and on Mac OSX Ventura and IM 7.1.1.32 Q16 did
I see nothing wrong with it. Do you see any issue on your portable version of IM 7 with that command? |
Beta Was this translation helpful? Give feedback.
-
Doing
So your original is 105705 bytes and your resized image is only 51176 bytes. So the resized is about half the original size. The original is
But the resized PNG8:x.png has the blue channel changed from 8-bit to 2-bit and the alpha from 8-bit to 1-bit. Both are indexed according to the PNG tags
|
Beta Was this translation helpful? Give feedback.
-
If I do
then I do see black spots around the green circles. That likely occurs because you have black background in the image, which can be seen by
If I try
This mitigates it but leaves some around. These are from antialiasing which leaves some pixels not perfectly black, but near black. The command -background white -alpha background only changes the pure black to white. I also tried magick deskcat8.png -fuzz 30% -fill "rgba(255,255,255,1)" -opaque "rgba(0,0,0,1)" -resize 1140x1102 -alpha deactivate -colors 255 -alpha activate yy.png Which did a little better but did not get all the black spots and changed some colors to white. If you lower the fuzz value it will get your colors back but keep more black spots. I am not sure what to suggest. You might try making the background white in your graphic when you create it in whatever tool that was. That may be less noticeable when resized. The only other course you have already tried with -filter point. |
Beta Was this translation helpful? Give feedback.
-
Try this with thresholding your alpha channel to avoid the blending with the antialiased pixels. It seems to work to avoid the back anti-aliased pixels.
The result is only 73840 bytes with
|
Beta Was this translation helpful? Give feedback.
-
ImageMagick version
6.9.11-60 Q16 x86_64 2021-01-25
Operating system, version and so on
Ubuntu 22.04
Description
Hi folks,
We're trying to sort out some long-standing issues in WordPress in regards to the resizing of paletted PNG images: https://core.trac.wordpress.org/ticket/36477#comment:53
Also connected to #3825, and we've tried a few different things without landing on a good solution.
As for versions tested, besides the 6.9.11 release, I've also tried the same solutions with 7.1.1-19
The main problem we're trying to solve, is that resizing a paletted image introduces more colors, and then the image can no longer use a palette, and the result is a larger file size than the original, anywhere up to 4-6x (or worse). Most of my testing of the various solutions has been with the image from #3825: https://ewwwio-downloads.b-cdn.net/png-tests/deskcat8.png
More examples:
https://ewwwio-downloads.b-cdn.net/png-tests/17-c3-duplicate-entries.png
https://ewwwio-downloads.b-cdn.net/png-tests/cloudflare-status.png
https://ewwwio-downloads.b-cdn.net/png-tests/Palette_icon.png
https://ewwwio-downloads.b-cdn.net/png-tests/rabbit-time-paletted.png
https://ewwwio-downloads.b-cdn.net/png-tests/test8.png
Here's some things I've tried:
Though this is supposed to have worked at some point, it now throws a fatal error, though this is caught by WP and we simply end up with no resized images/thumbnails:
PHP Fatal error: Uncaught ImagickException: Cannot write PNG8 or color-type 3; colormap is NULL '.../wp-content/uploads/2024/05/cat-desk8-10-300x290.png' @ error/png.c/WriteOnePNGImage/9666 in .../wp-includes/class-wp-image-editor-imagick.php:914
This only worked on Palette_icon.png, all the other images above still had resized images larger than the original.
The quantizeImage() method seemed the most promising, and works great if there is no transparency. I found this related post illustrating the issue: https://stackoverflow.com/questions/14031965/convert-32-bit-png-to-8-bit-png-with-imagemagick-by-preserving-semi-transparent/14032098#14032098
It doesn't seem to be 100% consistent, as test8.png comes out all right after quantizeImage().
I've tried altering the parameters to quantizeImage() to no avail. Dithering or not, expanded the tree all the way to 8 (maybe higher), but as soon as the image is quantized, the black pixels show up.
Is there some way to modify the alpha channel so that quantizeImage() will handle it better? Or is there some other solution we're overlooking?
For brevity, here is what we are trying to accomplish:
Beta Was this translation helpful? Give feedback.
All reactions