suda Senior Java EE Develope specialises in Pdf forms , Fonts, application servers and Image manipulation, meditates in spare time.

Lanczos3 algorithm as a way to produce better image downscaling

1 min read

Image scaling options

Recently at IDR Solutions, I have been looking for ways to provide higher image quality when down-sampling images in Java with our image library, JDeli. I stumbled across Lanczos3 and this gives excellent results. So the purpose of this article is to introduce the algorithm and show the better results you can obtain.

By default, Java offers a choice of algorithms to use when scaling down an image. The default ImageIO library in Java uses the image scaling algorithms nearest neighbour, bilinear and bicubic to scale images (with a trade-off between speed and quality).

Of these bicubic scaling (which takes into account other rows and columns) is the best (and slowest!) option.  But over 200% of the generated results are not good enough to maintain image clarity and essential details start to disappear. So I looked further afield and decided to use the Lanczos3 filtering algorithm. This is not available in ImageIO so I have had to implement it in our own Java image library, JDeli, to show you the results.

So let us look at some examples of all these scaling algorithms so you can see why I am such a fan of Lanczos3.

Image scaling examples

puzzle

[click on the image above to view the original image (2048×2048) ]

Let us consider the image above which is created in a simple paint application on windows and we resized the image into 128×128 dimensions with several algorithms

  1. java Nearest Neighbour (ImageIO)
  2. java bilinear (ImageIO)
  3. java bicubic (ImageIO)
  4. Lanczos3 (QualityThumbnail.java – examples produced using JDeli)

 

nearest-neighbour     bilinear    bicubic    lanczos

[click on any images above to view the thumbnail version (128×128) ]

With all of these algorithms, there is a trade-off between time and quality. The best results (BiCubic and Lanczos3) require the slightly more processing time than bicubic but produces the best results. Only Lanczos3 retains the key details of the image as we make is smaller.

The only downside with Lanczos3 is that it is not implemented in Java as a default option. You will either need to implement it yourself (details on Wikipedia) or use a third-party library such as our commercial JDeli Image Library.

In JDeli it is a single line code to get downscaled thumbnail version

BufferedImage thumbnail = QualityScaler.getScaledImage(sourceImage,128,128);

What does Quality Scaler do in JDeli?

Generates high-quality downsampled thumbnails and scaled images from Bufferedimage fastest convolution scaling in java.

Do you think the better results are worth the extra time?



Are you a Java Developer working with Image files?

// Read an image
BufferedImage bufferedImage = JDeli.read(dicomImageFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(heicImageFile);

// Write an image
JDeli.write(bufferedImage, "heic", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpegImageFile);

// Write an image
JDeli.write(bufferedImage, "jpeg", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpeg2000ImageFile);

// Write an image
JDeli.write(bufferedImage, "jpx", outputStreamOrFile);
// Write an image
JDeli.write(bufferedImage, "pdf", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(pngImageFile);

// Write an image
JDeli.write(bufferedImage, "png", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(tiffImageFile);

// Write an image
JDeli.write(bufferedImage, "tiff", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(webpImageFile);

// Write an image
JDeli.write(bufferedImage, "webp", outputStreamOrFile);
suda Senior Java EE Develope specialises in Pdf forms , Fonts, application servers and Image manipulation, meditates in spare time.

Should you travel by plane or train?

Why travel to Switzerland by train This year, my daughter set me the challenge to take the train rather than fly when I next...
Mark Stephens
1 min read

What is JBIG2?

JBIG2 is a lossless and lossy compression standard for bi-level images like scanned documents, offering high compression ratios by identifying and encoding similar shapes...
chika
47 sec read

2 Replies to “Lanczos3 algorithm as a way to produce better image…”

Comments are closed.