Question

I use the following code to convert a gif file to a jpg file, it works but the result jpg file isn't the same quality as the original gif file, why ? Any way to improve the quality ?

try
{
  ImageIO.write(ImageIO.read(new File("C:/abc.gif")),"jpg",new File("C:/abc.jpg"));
}
catch (Exception e) { e.printStackTrace(); }

So, to ask this question from another angle, with the above approach, how to increase the output quality ?

Was it helpful?

Solution

JPEG is a lossy image compression format which discards information that the human eyes can't notice very well. However, depending on the image quality settings when compressing the image, compression artifacts, such as blockiness and smudging can become visible -- this is the reason behind the image not appearing to be clear.

It may be possible that the ImageIO.write method is saving the JPEG with a lower quality setting than necessary to keep the image looking good enough.

In order to set the quality setting higher, it will be necessary to use the ImageWriteParam to specify the image quality settings, then use the ImageWriter to output the JPEG.

For example:

// Get a writer for JPEG.
ImageWriter iw = ImageIO.getImageWritersByFormatName("jpeg").next();
iw.setOutput(new FileImageOutputStream(outputFile));

// Set the compression quality to 0.9f.
ImageWriteParam iwParam = iw.getDefaultWriteParam();
iwParam.setCompressionQuality(0.9f);

// Write image
iw.write(null, new IIOImage(imageToWrite, null, null), iwParam);

Methods of interest:

That said, as the other answers mention, JPEG is unsuitable for compression of text and illustrations. If the goal is to preserve the image quality, but the file size can be bigger than with compression with JPEG, then using an lossless image compression format such as PNG would preserve the image quality as it is, without any loss. However, in many cases, the file size of a PNG file will be larger than a JPEG. (But this is not always the case.)

The general rule of thumb is, for photographs and other realistic images, lossy compression formats such as JPEG will work well for the file size vs. quality trade off, while lossless compression formats such as PNG will work better for illustrations and line art, where there are large areas of similar color with little gradient effects.

OTHER TIPS

JPEG is specially bad for images with text inside. A good alternative is .PNG.

JPEG guarantees some loss in fidelity; that's how it achieves compression. You could choose a different block size that would minimize the artifacts, but there will be artifacts.

GIF is lossless and JPEG is lossy; abc.jpg will never be pixel equivalent to abc.gif.

Roughly speaking, JPEG is suitable for images which are photographic in nature. If the image is one that contains large variations in colour, has naturalistic elements and resembles a live environment, then JPEG will provide good quality with no visible artifacts.

If the image is drawn, or contains large swathes of a single colour and is not photographic, then the image will be better compressed using PNG. This is a lossless format that can do 32 bit colour.

You should pretty much never use GIF format images these days - the format only supports 8 bit colour and provides relatively poor compression.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top