为什么我从 gif 文件转换成 jpg 文件不清晰?

发布于 2024-07-24 07:18:30 字数 290 浏览 8 评论 0 原文

我使用以下代码将 gif 文件转换为 jpg 文件,它可以工作,但结果 jpg 文件与原始 gif 文件的质量不同,为什么? 有什么办法可以提高质量吗?

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

那么,换个角度问这个问题,通过上述的做法,如何提高输出质量呢?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴骨ら 2024-07-31 07:18:30

JPEG 是一个 有损图像压缩格式,丢弃人眼无法很好注意到的信息。 但是,根据压缩图像时的图像质量设置,压缩伪像,例如块状和污迹可以变得可见——这就是图像看起来不清晰的原因。

ImageIO.write 方法正在以低于保持图像美观所需的质量设置保存 JPEG足够的。

为了将质量设置设置得更高,需要使用 ImageWriteParam 指定图像质量设置,然后使用 ImageWriter 输出 JPEG。

例如:

// 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);

感兴趣的方法:

也就是说,正如其他答案提到的,JPEG 是不适合文本和插图的压缩。 如果目标是保持图像质量,但文件大小可能大于 JPEG 压缩,则使用 无损图像压缩格式,例如PNG将保持图像质量不变,没有任何损失。 然而,在许多情况下,PNG 文件的文件大小会大于 JPEG。 (但情况并非总是如此。)

一般经验法则是,对于照片和其他真实图像,有损压缩格式(例如 JPEG)可以很好地实现文件大小与质量的权衡,而无损压缩格式(例如 PNG)对于插图和线条艺术来说效果更好,其中有大面积的相似颜色且渐变效果很小。

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.

我很坚强 2024-07-31 07:18:30

JPEG 对于内部有文本的图像尤其不利。 .PNG 是一个不错的选择。

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

南风起 2024-07-31 07:18:30

JPEG 保证保真度有所损失; 这就是它实现压​​缩的方式。 您可以选择不同的块大小来最大限度地减少伪影,但仍然会存在伪影。

GIF是无损的,JPEG是有损的; abc.jpg 永远不会与 abc.gif 像素等效。

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.

半仙 2024-07-31 07:18:30

粗略地说,JPEG 适用于摄影性质的图像。 如果图像包含较大的颜色变化、具有自然元素并且类似于现场环境,则 JPEG 将提供良好的质量,没有可见的伪影。

如果图像是绘制的,或者包含大片单一颜色且不是摄影图像,则使用 PNG 可以更好地压缩图像。 这是一种无损格式,可以实现 32 位颜色。

现在你几乎不应该使用 GIF 格式的图像 - 该格式仅支持 8 位颜色并且提供相对较差的压缩。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文