views:

375

answers:

4

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 ?

+5  A: 

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.

Oliver N.
There is a lossless part of the JPEG standard, yet nobody bothered to implement ist :)
Joey
Although GIF is lossless, it's worth noting that it is a lossless _indexed_ colour format, so some colour information is lost when saving from a Truecolor source. JPEG is a lossy truecolor format so the indexed image is upconverted to truecolor (without improvement in quality), then "averaged out" in a JPEG/DCT sense (with a corresponding decrease in quality).
Stobor
There is also a lossy part of the GIF standard. Sigh, things aren't so simple :)
zildjohn01
+17  A: 

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.

coobird
Frank
@Frank: Even at compression quality of 1.0f, the compression will be lossy, so the quality of the output will not be identical to the original. I have observed changes to the color in the past, so that's probably one of the effects of the lossy compression.
coobird
+9  A: 

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

tekBlues
+2  A: 

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.

1800 INFORMATION