You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
premultiplied Usage: When compress png with premulitiplied, then output png shows bad. #4413
I wrote it , just for test the premultiplied usage. Is that possible to compress the png to support premultiplied ? Instead I use the tinypng to compress the png, the output png shows good.
I think the best thing for you to do is create a complete, minimal, standalone repo with code and image(s) that allows someone else to reproduce and therefore understand what you're looking for.
pic-compressShowBad.zip
Here is the minimal repo for you. There are two pics under folder pic-compressShowBad\assets\main\native\4a, the one named "4a28a44c-8feb-4ca5-9913-9dd8d2b023c7" shows bad which compressed by sharp, the other one "PariteShip-goodPic" shows good which is the original picture.
This looks like an artefact of the quantisation process used to reduce the number of colours in an image, hence my original comment about palette size.
You might have some luck removing the use of dithering by setting dither to zero.
.png({palette: true,dither: 0})
If this doesn't produce suitable output, I'd probably avoid palette-based PNG entirely given quality appears to be of your greatest concern.
The output still the same. The original picture has been premultiplied Alpha, how can I keep it? Of course, when I remove premultiplied Alpha, the output turns right. My greatest concern is use the sharp to shrink the picture size meanwhile it can still keep the premultiplied Alpha result.
The premultiplied input property you've highlighted is for raw pixel based input, which you do not appear to be using.
The premultiplied output property you've highlighted tells you whether any operations in the pipeline resulted in sharp applying a premultiply/unpremultiply roundtrip whilst processing, as documented by toFile:
info contains the output image format, size (bytes), width, height, channels and premultiplied (indicating if premultiplication was used).
As I mentioned above, I think the best thing for you to do is create a complete, minimal, standalone repo with code and image(s) that allows someone else to reproduce and therefore understand what you're looking for.
As I mention before, the original pic is premultiplied one. I use raw.premultiplied option to avoid sharp premultiplying the image, but the output file which I copy to the folder pic-compressShowBad\assets\main\native\4a and renamed is as "4a28a44c-8feb-4ca5-9913-9dd8d2b023c7" shows bad.
Activity
lovell commentedon Jun 12, 2025
Is this LLM-generated code? None of these functions accept
premultipliedas an option.When you provide a
qualityoption topng()you are selecting to use lossy, quantised, palette-based output.https://sharp.pixelplumbing.com/api-output/#png
tangkaikk commentedon Jun 12, 2025
I wrote it , just for test the premultiplied usage. Is that possible to compress the png to support
premultiplied? Instead I use the tinypng to compress the png, the output png shows good.lovell commentedon Jun 12, 2025
I think the best thing for you to do is create a complete, minimal, standalone repo with code and image(s) that allows someone else to reproduce and therefore understand what you're looking for.
lovell commentedon Jul 7, 2025
@tangkaikk Were you able to make any progress with this? If you still require help, please can you provide the requested info.
tangkaikk commentedon Jul 16, 2025
pic-compressShowBad.zip
Here is the minimal repo for you. There are two pics under folder pic-compressShowBad\assets\main\native\4a, the one named "4a28a44c-8feb-4ca5-9913-9dd8d2b023c7" shows bad which compressed by sharp, the other one "PariteShip-goodPic" shows good which is the original picture.
The compress code is below:
lovell commentedon Jul 16, 2025
Thanks, as I mentioned above, when you provide a
qualityoption topng()you are selecting to use lossy, quantised, palette-based output.https://sharp.pixelplumbing.com/api-output/#png
The sample image provided is already a palette-based image that has the maximum 256 palette entries (768 bytes is 256 RGB entries):
By selecting a quality of 80 you are reducing the palette size (474 bytes is 158 RGB entries):
I would suggest you use
palette: trueand do not provide aquality:tangkaikk commentedon Jul 16, 2025
Use the suggestion, the problem still exist.
lovell commentedon Jul 16, 2025
This looks like an artefact of the quantisation process used to reduce the number of colours in an image, hence my original comment about palette size.
You might have some luck removing the use of dithering by setting
ditherto zero.If this doesn't produce suitable output, I'd probably avoid palette-based PNG entirely given quality appears to be of your greatest concern.
tangkaikk commentedon Jul 17, 2025
The output still the same. The original picture has been premultiplied Alpha, how can I keep it? Of course, when I remove premultiplied Alpha, the output turns right. My greatest concern is use the sharp to shrink the picture size meanwhile it can still keep the premultiplied Alpha result.
tangkaikk commentedon Jul 17, 2025
I set the premultiplied option true, but output info it turns false.
lovell commentedon Jul 17, 2025
The
premultipliedinput property you've highlighted is for raw pixel based input, which you do not appear to be using.The
premultipliedoutput property you've highlighted tells you whether any operations in the pipeline resulted in sharp applying a premultiply/unpremultiply roundtrip whilst processing, as documented by toFile:As I mentioned above, I think the best thing for you to do is create a complete, minimal, standalone repo with code and image(s) that allows someone else to reproduce and therefore understand what you're looking for.
tangkaikk commentedon Jul 18, 2025
As I mention before, the original pic is premultiplied one. I use raw.premultiplied option to avoid sharp premultiplying the image, but the output file which I copy to the folder pic-compressShowBad\assets\main\native\4a and renamed is as "4a28a44c-8feb-4ca5-9913-9dd8d2b023c7" shows bad.
Here is the code I use: