Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting low quality thumbnail from a high quality image in Java

I have tried the below code to generate the high quality thumbnail image but got the thumb nail blurred and not that much clarity.

BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setBackground(Color.WHITE);
graphics2D.setPaint(Color.WHITE); 
graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
graphics2D.setComposite(AlphaComposite.Src);
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
graphics2D.dispose();      
File file = new File(thumbnailFile);
if (javax.imageio.ImageIO.write(thumbImage, "JPG", file))
    return file;
}

The original image is a high quality image. Why am I getting the thumbnail image distorted and low in quality?

like image 499
Antony Avatar asked Mar 12 '26 11:03

Antony


1 Answers

There are two possibilities from looking at the code:

  1. The original image is very large, and the thumbnail is very small, resulting in image quality being degraded by the simple bilinear interpolation.

  2. The compression artifacts resulting from the JPEG encoding is degrading the quality of the thumbnail.

Using a simple bilinear interpolation can be adequate if the original image and the thumbnail doesn't differ in size by a whole lot, for example, going from a 200x200 to 100x100.

However, when it comes to resizing images that are large (such as 1600x1200) to a thumbnail size image, bilinear interpolation (and bicubic interpolation, for that matter), so alternative techniques such as multi-step resizing should be used.

The article The Perils of Image.getScaledInstance() by Chris Campbell goes into more details about how and why downscaling large images can lead to degradation of image quality.

The book Filthy Rich Clients by Chet Haase and Romain Guy also goes into some details about creating high-quality thumbnails.


I maintain a thumbnail generation library called Thumbnailator, which uses techniques such as multi-step resizing to create high-quality thumbnails with an easy-to-use API.

For example, your example code could be written using Thumbnailator like this:

Thumbnails.of(image)
  .size(thumbWidth, thumbHeight)
  .outputFormat("JPG")
  .toFile(file);

It's also possible to specify the compression quality settings if compression artifacts are causing image quality degradation:

Thumbnails.of(image)
  .size(thumbWidth, thumbHeight)
  .outputFormat("JPG")
  .outputQuality(0.9)
  .toFile(file);
like image 116
coobird Avatar answered Mar 14 '26 02:03

coobird



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!