ImageSharp icon indicating copy to clipboard operation
ImageSharp copied to clipboard

Pad loses image quality

Open barteksekula opened this issue 4 years ago • 4 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have verified that I am running the latest version of ImageSharp
  • [X] I have verified if the problem exist in both DEBUG and RELEASE mode
  • [X] I have searched open and closed issues to ensure it has not already been reported

Description

Pad operation loses quality. However, when padding the image first to fit a bigger frame and resizing later seems to produce much higher quality outputs.

Steps to Reproduce

foo.jpg (test source image) foo

var thumbnailWidth = 150;
var thumbnailHeight = 80;
var factor = 3;
using (Image image = Image.Load(@"D:\foo.jpg"))
{
	image.Mutate(x => x.Pad(thumbnailWidth, thumbnailHeight, Color.Transparent));
	image.Save(@"D:\padded.jpg");
}

produces: padded

using (Image image = Image.Load(@"D:\foo.jpg"))
{
	image.Mutate(x => x.Pad(thumbnailWidth * factor, thumbnailHeight * factor, Color.Transparent).Resize(x.GetCurrentSize().Width / factor, x.GetCurrentSize().Height / factor));
	image.Save(@"D:\padded_and_resized.jpg");
}

produces: padded_and_resized

The output byte sizes and resolutions are the same.

System Configuration

  • ImageSharp version: most recent 1.0.4
  • Other ImageSharp packages and versions: none
  • Environment (Operating system, version and so on): Windows 10
  • .NET Framework version: 5.0.104
  • Additional information: none

barteksekula avatar Dec 30 '21 12:12 barteksekula

This is expected. You're incorrectly downscaling an image using an operation designed to increase the size of a canvas.

Pad uses nearest neighbor resampling in order to ensure that the original content is left unchanged during the underlying resampling operation that generates the new canvas. By downscaling you are applying a low quality resampling operation on the original pixels.

TL;DR

Don't use Pad for downscaling. It's for evenly padding an image to fit a larger canvas.

JimBobSquarePants avatar Dec 30 '21 12:12 JimBobSquarePants

Thanks for your response. I just want any image to fit into 150x80 placeholder and fill with transparency if needed (if aspect ratio is different). Pad seems the right approach if the source image is smaller than 150x80 however if the source is bigger would I need to resize first keeping the aspect ratio and then try to fill in a 150x80 frame with transparent color with the result of that resize operation?

barteksekula avatar Dec 30 '21 13:12 barteksekula

	image.Mutate(x =>
	   x.Resize(new ResizeOptions
	   {
		   Size = new Size(thumbnailWidth, thumbnailHeight),
		   Mode = ResizeMode.Pad
	   }).BackgroundColor(Color.Transparent));

seems to do the job. sorry for trouble

barteksekula avatar Dec 30 '21 13:12 barteksekula

Skip the .BackgroundColor(Color.Transparent) call as wont be doing anything helpful to you but will still end up using CPU resources.

The BackgroundColor() operation applies a pixel blending operation on all the pixels which effectively replaces all the transparent pixels with the color you provide, in this case transparent, so your effectively replacing all the existing transparent pixels with a transparent pixel thus net effect of no perceivable difference except we iterate all pixels to make no perceivable changes.

tocsoft avatar Dec 30 '21 13:12 tocsoft