Resizing an Image Without Losing Any Quality

How to resize image without losing quality

I'm using the following method for thousands of images and it never loses significant quality or results in a dotted image.

public static Image ScaleImage(Image image, int height)
{
double ratio = (double)height/ image.Height;
int newWidth = (int)(image.Width * ratio);
int newHeight = (int)(image.Height * ratio);
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(newImage))
{
g.DrawImage(image, 0, 0, newWidth, newHeight);
}
image.Dispose();
return newImage;
}

I've taken the liberty of scaling the image you posted using this code to 128px (Like the thumbnail you posted).

Result:

Sample Image

How resize image without losing quality

I use this method as a way to get a thumbnail image (of any size) from an original (of any size). Note that there are inherent issues when you ask for a size ratio that varies greatly from that of the original. Best to ask for sizes that are in scale to one another:

public static Image GetThumbnailImage(Image OriginalImage, Size ThumbSize)
{
Int32 thWidth = ThumbSize.Width;
Int32 thHeight = ThumbSize.Height;
Image i = OriginalImage;
Int32 w = i.Width;
Int32 h = i.Height;
Int32 th = thWidth;
Int32 tw = thWidth;
if (h > w)
{
Double ratio = (Double)w / (Double)h;
th = thHeight < h ? thHeight : h;
tw = thWidth < w ? (Int32)(ratio * thWidth) : w;
}
else
{
Double ratio = (Double)h / (Double)w;
th = thHeight < h ? (Int32)(ratio * thHeight) : h;
tw = thWidth < w ? thWidth : w;
}
Bitmap target = new Bitmap(tw, th);
Graphics g = Graphics.FromImage(target);
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.High;
Rectangle rect = new Rectangle(0, 0, tw, th);
g.DrawImage(i, rect, 0, 0, w, h, GraphicsUnit.Pixel);
return (Image)target;
}

Re-sizing an image without losing quality

The best article I have ever read on this topic is The Perils of Image.getScaledInstance() (web archive).

In short: You need to use several resizing steps in order to get a good image. Helper method from the article:

public BufferedImage getScaledInstance(BufferedImage img,
int targetWidth,
int targetHeight,
Object hint,
boolean higherQuality)
{
int type = (img.getTransparency() == Transparency.OPAQUE) ?
BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage ret = (BufferedImage)img;
int w, h;
if (higherQuality) {
// Use multi-step technique: start with original size, then
// scale down in multiple passes with drawImage()
// until the target size is reached
w = img.getWidth();
h = img.getHeight();
} else {
// Use one-step technique: scale directly from original
// size to target size with a single drawImage() call
w = targetWidth;
h = targetHeight;
}

do {
if (higherQuality && w > targetWidth) {
w /= 2;
if (w < targetWidth) {
w = targetWidth;
}
}

if (higherQuality && h > targetHeight) {
h /= 2;
if (h < targetHeight) {
h = targetHeight;
}
}

BufferedImage tmp = new BufferedImage(w, h, type);
Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
g2.drawImage(ret, 0, 0, w, h, null);
g2.dispose();

ret = tmp;
} while (w != targetWidth || h != targetHeight);

return ret;
}

Reduce the image size without losing its quality in photoshop

You can shrink image without losing quality in 3 ways:

  1. Photoshop:

    • Launch Photoshop then click on image > image size. Now you can select width and height as per your choice. Don't forget to tick on Bicubic sharper (reduction).
  2. Microsoft Paint:

    • First of all, launch Paint and then select resize. And now you can adjust width and height of your images.
  3. Online Tools: There are numerous websites available across the web by which you can easily shrink all your images without losing quality.

Reference:
http://www.hugestreet.info/2016/12/shrink-images-without-losing-quality.html

How to resize image without losing pixel qualityin python?

Use:

im = im.resize((521,512), resample=Image.NEAREST)

for that effect.

It is not really a "loss of quality" that you are seeing - it is actually a difference in the interpolation method. When upsizing an image, the algorithm effectively has to "invent" new pixels to fill the bitmap raster. Some algorithms interpolate between known surrounding values, other algorithms just take the closest value - also known as "Nearest Neighbour". There are both advantages and disadvantages - "Nearest Neighbour" will be faster and will not introduce new "in-between" colours into your resized image. On the downside, it will be slower, look more "blocky" and less smooth.

It requires some thought and experience to choose the appropriate method.



Related Topics



Leave a reply



Submit