zale.dymond
zale.dymond

Reputation: 1

Image resize - bad quality

I want to scale image from 239x210 to 22x19, I am using Thumbnailator:

public BufferedImage process(BufferedImage bufferedImage, CanvasData canvasData) {
    int imageSize = (canvasData.getDensity() * canvasData.getSize()) / 10;

    try {
        bufferedImage = Thumbnails.of(bufferedImage)
                .imageType(BufferedImage.TYPE_INT_RGB)
                .size(imageSize, imageSize)
                .outputQuality(1)
                .asBufferedImage();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bufferedImage;
}

Gimp ||| Original ||| Thumnailator

I want to have the same output like from gimp, but I don't know how can I reach that.

Upvotes: 0

Views: 1080

Answers (1)

coobird
coobird

Reputation: 160964

As Joop mentioned in the comments, the main reason for the poor image quality is because there's a lot of compression artifacts compared to the output from GIMP.

This is because you're using the outputQuality method that controls the compression ratio of the output, but are not writing the thumbnail to a file, but to a BufferedImage via the asBufferedImage method.

If you take a close look at the documentation for the outputQuality method you'll find it mentions that this method will not have any effect to the final image.

So, you have two options:

  1. Use one of Thumbnailator's output methods that writes to a File or OutputStream so that you get JPEG stream, which honors your outputQuality(1.0) setting. The toFile, asFile, toOutputStream, asOutputStream methods will be your ticket for this option.

  2. If you have to output to a BufferedImage, then you'll need to configure whatever if actually writing that BufferedImage to write a JPEG with a lower compression ratio -- that's happening outside of the code snippet you provided.

I'd recommend option 1, because it seems like you're communicating the intent that you want a JPEG with minimal compression to Thumbnailator, so it would make sense to let Thumbnailator write the JPEG to a file or stream.


Disclaimer: I'm the maintainer of the Thumbnailator library.

Upvotes: 1

Related Questions