Imagesharp: Very bad result after resize.

Created on 9 Apr 2017  路  9Comments  路  Source: SixLabors/ImageSharp

Hi, I use ImageSharp 1.0.0-alpha5-00054 with ASP.NET Core 1.1

I use the following code to resize the image:

var result = new MemoryStream();

var options =
    new ResizeOptions
    {
        Size = new Size(230, 140),
        Mode = resizeMode
    };

Image.Load(input).Resize(options).Save(result);

And the result is very bad, see the attached source image (login-icon) and resized image:

login-icon
8bf49d97-ba7a-4eec-b056-b9b9700298e3_0_230_140_boxpad

I have no idea what I do wrong.

formats bug

All 9 comments

Hi @SebastianStehle

There's nothing wrong with what you're doing. It seems that the way we quantize the image to reduce the color palette when encoding that image (It's an indexed png with 183 entries in the alpha palette table) can't seem to handle the distribution of alpha values.

It's frustrating to discover this as I thought we had png pretty much 100% correct. I'v been experimenting for a couple of hours now to no avail.

Cheers

James

I see.

I have a lot of examples that look so bad and I think that I optimized all of them with the following tool: https://tinypng.com/

Hope it helps.

Thanks for the awesome job.

Btw: I wrote something similar (but less advanced) some years ago: https://imagetools.codeplex.com/

try

c# using (Image img = Image.Load(input)) { var options = new ResizeOptions { Size = new Size(230, 140), Mode = resizeMode }; img.MetaData.Quality = 0; // reset the Quality to 0 to force us not to generate an Indexed PNG img.Resize(options).Save(result); }

looks like we are retaining a "Quality" measurement between decoding and encoding and our Indexed encoding is only allowing a single masked transparency channel thus we loose the variable opacity of the original when we re-encode.

Thank you very much, it solves the problem for me.

Oh wow @SebastianStehle you're the reason I thought this library was possible. If it hadn't been for your prior work I would never have dreamed of attempting it. Thank you! 馃憤

Back to the issue... @tocsoft Our encoder actually supports more than one transparent index, it's just that when the transparent threshold is set to the default of 0 we only add values that fall below that threshold.

I believe we might actually be doing something wrong when both capturing and encoding transparent values in indexed png's . I'll need to have a look at the spec again and probably the source for libpng also. If either of you feel like investigating the issue in the interim though please do.

I have same issue also I right your code but it's very bad quality.

         image.Mutate(x=>x.Resize(new ResizeOptions()
            {
                Mode = ResizeMode.Max,
                Size = new Size((int)(image.Width * scale), (int)(image.Height * scale))
            } )) ;
            image.MetaData.HorizontalResolution = 0d;
 IImageEncoder _enc;
   _enc = new JpegEncoder()
                    {
                        Quality = 100,
                        IgnoreMetadata = true
                    };

before :
a8fb299d3952479dbd0978a3083d1ada9227369fd91fb61bfe5a80ff33c21e27

after:

lg4c3c1383-d643-457e-b9c0-f890594eb133

Hey @a-jahanshahlo don't hijack old issues like this. Your issue is completely unrelated.

I also cannot replicate your issue. Here's the output of beta5 on my machine.
resize

Hey @JimBobSquarePants in my project I should store image as binary on database. after that I create an Original size from binary but unfortunately when I resize to original size It lose quality.

Also I use beta 5 version with Windows OS, please advice me. am I wrong?

Questions on Gitter please.

Was this page helpful?
0 / 5 - 0 ratings