Closed PerplexDaniel closed 4 months ago
@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);
@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.
I thinkt his would have been better suited as a discussion.
Apologies, it felt like a bug compared to cwebp
but I didn't know about the FileFormat
setting. Feel free to move / close this of course.
I don't exactly know how Umbraco sets things up but it's likely not using a custom Configuration
instance so using Configuration.Default
. It's possible to update the default instance by registering your own encoders.
You'd put this somewhere in startup.
Configuration.Default.ImageFormatsManager.SetEncoder(WebpFormat.Instance, new WebpEncoder() { FileFormat = WebpFileFormatType.Lossy });
Prerequisites
DEBUG
andRELEASE
modeImageSharp version
3.1.4
Other ImageSharp packages and versions
N/A
Environment (Operating system, version and so on)
Windows 11 23H2 Build 22631.3737
.NET Framework version
8.0
Description
When converting some PNG files to WebP using ImageSharp the output file is way larger than when using
cwebp
1.4.0 (Google's WebP encoder).For example, when the attached 9.01 MB PNG file is converted to WebP using ImageSharp the output image is 7.14 MB whereas
cwebp
creates an image of 392 kB, 18x smaller.Setting a different quality on
WebpEncoder
does not seem to have any effect either which is strange.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
img.png
on your computerdotnet new console -n ImageSharpTest
):cd ImageSharpTest
dotnet add package SixLabors.ImageSharp
Directory.SetCurrentDirectory("D:\"); // Change to directory holding
img.png
using Image image = Image.Load("img.png"); image.SaveAsWebp("img.webp");