PNG to WebP conversion: some files 10x larger than with cwebp
#2766
-
Prerequisites
ImageSharp version3.1.4 Other ImageSharp packages and versionsN/A Environment (Operating system, version and so on)Windows 11 23H2 Build 22631.3737 .NET Framework version8.0 DescriptionWhen converting some PNG files to WebP using ImageSharp the output file is way larger than when using For example, when the attached 9.01 MB PNG file is converted to WebP using ImageSharp the output image is 7.14 MB whereas Setting a different quality on I have seen this happen to random other PNG files too but obviously cannot conclude it happens to every PNG image out there. Steps to Reproduce
using SixLabors.ImageSharp;
Directory.SetCurrentDirectory("D:\\"); // Change to directory holding `img.png`
using Image image = Image.Load("img.png");
image.SaveAsWebp("img.webp");
If you downloaded Precompiled WebP utilities and library from Google, you can compare this to
Images |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
@PerplexDaniel I thinkt his would have been better suited as a discussion. It's likely cwep is using lossy encoding. I can use the default lossy encoding options with the following code and return a 387Kb image. using var img = Image.Load(@"C:\Users\james\Downloads\2765.png");
WebpEncoder encoder = new ()
{
FileFormat = WebpFileFormatType.Lossy
};
img.SaveAsWebp($@"C:\Users\james\Downloads\2765.webp", encoder); |
Beta Was this translation helpful? Give feedback.
-
@JimBobSquarePants Thank you for the super fast response. Is there any way to configure Lossy as a default? This is used in the context of Umbraco CMS if that matters.
Apologies, it felt like a bug compared to |
Beta Was this translation helpful? Give feedback.
-
I don't exactly know how Umbraco sets things up but it's likely not using a custom You'd put this somewhere in startup. Configuration.Default.ImageFormatsManager.SetEncoder(WebpFormat.Instance, new WebpEncoder() { FileFormat = WebpFileFormatType.Lossy }); |
Beta Was this translation helpful? Give feedback.
-
It seems this behavior has changed between 2.1.8 and 3.1.4, presumably 2.x to 3.x. If I run the same code (without passing the encoder to SaveAsWebP) in 2.1.8 I get a ~400KB file. Updating to 3.1.4 it is 7MB. So presumably in 2.x the default was actually using var img = Image.Load("img.png");
img.SaveAsWebp("img.webp");
// img.webp size:
// v2.1.8: 404 KB
// v3.1.4: 7.14 MB |
Beta Was this translation helpful? Give feedback.
@PerplexDaniel I thinkt his would have been better suited as a discussion.
It's likely cwep is using lossy encoding.
I can use the default lossy encoding options with the following code and return a 387Kb image.