硬汉嵌入式论坛

 找回密码
 立即注册
查看: 4412|回复: 2
收起左侧

[LibJPEG] 【LibJPEG专题系列九】GIF图像转换为JPEG的经验法则

[复制链接]

740

主题

1326

回帖

3546

积分

管理员

春暖花开

Rank: 9Rank: 9Rank: 9

积分
3546
QQ
发表于 2015-4-21 16:14:41 | 显示全部楼层 |阅读模式
                           Section - [9] What are some rules of thumb for
                                       converting GIF images to JPEG?

Converting GIF files to JPEG is a tricky business --- you are piling one set of limitations atop a quite different set,
and the results can be awful. Certainly a JPEG made from a GIF will never be as good as a JPEG made from true
24-bit color data.  But if what you've got is GIFs, and you need to save space, here are some hints for getting the
best results.


With care and a clean source image, it's often possible to make a JPEG of quality equivalent to the GIF.  This does
not mean that the JPEG looks pixel-for-pixel identical to the GIF --- it won't.  Especially not on an 8-bit display,
because the color quantization process used to display the JPEG probably won't quite match the quantization
process used to make the GIF from the original data (see "[8] What is color quantization?").  But remember that
the GIF itself is not all that faithful to the full-color original, if you look at individual pixels.  Looking at the overall
image, a converted JPEG can look as good as its GIF source.  Some people claim that on 24-bit displays, a carefully
converted JPEG can actually look better than the GIF source, because dither patterns have been eliminated.  (More
about dithering in a moment.)


On the other hand, JPEG conversion absolutely *will* degrade an unsuitable image or one that is converted carelessly.  
If you are not willing to take the amount of trouble suggested below, you're much better off leaving your GIF images
alone.  Simply cranking the JPEG quality setting up to a very high value wastes space (which defeats the whole point
of the exercise, no?) and some images will be degraded anyway.


The first rule is never to convert an image that's not appropriate for JPEG (see "[3] When should I use JPEG, and
when should I stick with GIF?"). Large, high-visual-quality photographic images are usually the best source material.  
And they take up lots of space in GIF form, so they offer significant potential space savings.  (A good rule of thumb is
not to bother converting any GIF that's much under 100 Kbytes; the potential savings isn't worth the hassle.)


The second rule is to know where the image came from.  Repeated GIF<=>JPEG conversions are guaranteed to turn
an image into mush, because you pay a steep quality price on each round trip.  Don't reconvert images that have been
converted before.


The third rule is to get rid of the border.  Many people have developed an odd habit of putting a large single-color
border around a GIF image. While useless, this is nearly free in terms of storage cost in GIF files. It is *not* free in
JPEG files, either in storage space or in decoding time. Worse, the sharp border boundary can create visible artifacts
(ghost edges). Furthermore, when viewing a bordered JPEG on an 8-bit display, the quantizer will think the border
color is important because there's so much of it, and hence will waste color palette entries on the border, thus actually
reducing the displayed quality of the main part of the image!  So do yourself a favor and crop off any border before
JPEGing.


The final rule is to look at each JPEG, to make sure you are happy with it, before throwing away the corresponding
GIF.  This will give you a chance to re-do the conversion with a higher quality setting if necessary.  Also compare
the file sizes --- if the image isn't suitable JPEG material, a JPEG file of reasonable quality may come out *larger*
than the GIF.


Gray-scale photos usually convert without much problem.  When using cjpeg, be sure to use the -gray switch.
(Otherwise, cjpeg treats a GIF as color data; this works, but it wastes space and time if the image is really only
gray-scale.)  Quality settings around the default (75) are usually fine.


Color images are much trickier.  Color GIFs of photographic images are usually "dithered" to fool your eye into
seeing more than the 256 colors that GIF can actually store.  If you enlarge the image, you will find that adjacent
pixels are often of significantly different colors; at normal size the eye averages these pixels together to produce
the illusion of an intermediate color value.  The trouble with dithering is that, to JPEG, it looks like high-spatial-
frequency color noise; and JPEG can't compress noise very well.  The resulting JPEG file is both larger and of lower
image quality than what you would have gotten from JPEGing the original full color image (if you had it).  To get
around this, you need to "smooth" the GIF image before compression.  Smoothing averages together nearby pixels,
thus approximating the color that you thought you saw anyway, and in the process getting rid of the rapid color
changes that give JPEG trouble.  Proper use of smoothing will both reduce the size of the compressed file and give
you a better-looking output image than you'd get without smoothing.


With the IJG JPEG software (cjpeg or derived programs), a simple smoothing capability is built in.  Try "-smooth
10" or so when converting GIFs. Values of 10 to 25 seem to work well for high-quality GIFs.  GIFs with heavy-
handed dithering may require larger smoothing factors.  (If you can see regular fine-scale patterns on the GIF
image even without enlargement, then strong smoothing is definitely called for.)  Too large a smoothing factor
will blur the output image, which you don't want.  If you are an image processing wizard, you can also do smoothing
with a separate filtering program, but appropriate use of such tools is beyond the scope of this FAQ.


Quality settings around 85 (a bit higher than default) usually work well when converting color GIFs, assuming that
you've picked a good smoothing factor.  You may need still higher quality settings if you can't hide the dithering
pattern with a reasonable smoothing factor.  Really badly dithered GIFs are best left as GIFs.


Don't expect JPEG files converted from GIFs to be as small as those created directly from full-color originals.  
The dithering noise wastes space, but you won't be able to smooth away all the noise without blurring the image.
Typically, a good-quality converted JPEG will be one-half to one-third the size of the GIF file, not one-fourth as
suggested in section 4.  If the JPEG comes out much more than half the size of the GIF, this is a good sign that
the image shouldn't be converted at all.


The upshot of all this is that "cjpeg -quality 85 -smooth 10" is probably a good starting point for converting
color GIFs.  But if you care about the image, you'll want to check the results and maybe try a few other settings.
Blindly converting a large GIF library at this or any other setting is a recipe for disaster.
努力打造安富莱高质量微信公众号:点击扫描图片关注
回复

使用道具 举报

740

主题

1326

回帖

3546

积分

管理员

春暖花开

Rank: 9Rank: 9Rank: 9

积分
3546
QQ
 楼主| 发表于 2015-4-24 14:00:29 | 显示全部楼层
                             Section 9—将GIF图像转换为JPEG的经验法则?

    将GIF文件转换为JPEG是件棘手的事。你把一堆限制的规则运用到一个完全不同的规则上,结果肯定是很可怕的。
当然,由GIF转换而来的JPEG永远不会和24位色彩数据构成的JPEG质量一样好。但是,如果你得到的是GIF图像,又
需要节省空间,这里有一些提示可以获得最好的结果。


    有干净的原图像以及足够的细心,对于GIF图像,有可能做出一个质量相当的JPEG图像。这不意味着JPEG图像与
GIF看起来是像素对像素相同的——它从来也不会。特别不会发生在8位显示器上,因为用于显示JPEG图像的色彩量
化处理可能不会与从原始数据制作GIF的色彩量化处理相匹配。(见[8] 色彩量化)但是,需要记住的是,如果你看单
独像素的话,GIF本身并不是忠于全彩原型的。浏览整个图像,转换后的JPEG看起来可以和它的GIF源一样好。有一
些人声称,在24位显示器上,一个精心转换后的JPEG可以比它的GIF源看起来更好,原因就在于抖动模式已经被消除。
(稍后会有更多关于抖动的信息。)


    另一方面,JPEG转化绝对会降低一个不适合的图像或是粗心转换后的图像。如果你不愿意承担下面建议的麻烦数
最好还是把你的GIF图像晾在一边。简单地起动JPEG的质量设置提高到一个非常高的值将会浪费空间(这违背了转换
的重点)而且一些图像无论如何将会退化。


    第一条规则:永远不要转换不适合JPEG格式的图像。(详见[3] JPEGGIF的选择)大型、高视觉质量的摄影图像
通常是最好的素材。而且在GIF格式中,它们会占据大量的空间,因此,它们会提供显著的潜在空间节省。(一个好
的经验法则是不要费劲去转换远小于100 KBGIF文件,这个潜在的空间节省不值得这么麻烦。)


    第二条规则:了解图像的来源。反复的GIF<=>JPEG转换保证会将图像变得模糊,因为你会在每次的转换中都会
以很大的图像质量作为代价。总之,不要转换那些之前已被转换过的图像。


    第三条规则:去掉边界。很多人有一个小的习惯--放一个大的单色边界在GIF图像周围。虽然毫无用处,但这在
GIF文件的存储成本方面几乎是免费的。而对于JPEG文件,无论是存储空间还是解码时间上,这都不是免费的。更
糟的是,明显的边界可以制造可见的伪像(ghost edges)。此外,当在8位显示器上浏览一个有边界的JPEG图像时,
色彩量化会由于边界数量很多因而觉得边界的颜色很重要,由此会浪费边界的调色板项目,这样也就在实际上减少了
图像主体部分的显示质量!因此,算是帮你自己的忙并且在JPEG转换之前去掉所有的边界。


    最后的规则:在你丢弃相应的GIF之前,看看每个JPEG,以确保您的满意。如有必要,这会给你一个重做高质量
设置转换的机会。同时也可比较文件的大小——如果该图像不是JPEG的合适素材,合理质量的JPEG文件有可能会比
GIF的更大一些。


    灰度图像的转换通常没有太大的问题。当使用cjpeg时,确保使用-gray命令。(否则,cjpeg会将GIF视为色彩
数据;这也行得通,但是如果图像只是灰度时,就会浪费空间和时间了。)默认值(75)的质量设置通常就比较好。


    彩色图像要复杂得多。彩色GIF格式的摄影图像经常通过“抖动”来欺骗你的眼睛看到多于GIF实际可以存储的256
种的颜色。如果你放大图像就会发现,相邻像素通常是明显不同的颜色;正常大小的图像时,眼睛会将这些像素平均
开来,产生一个中间色值的错觉。抖动的问题在于,对于JPEG来说,它看起来像是高空间频率的颜色噪声,而且
JPEG不能很好的压缩噪声。由此产生的JPEG文件将会比你预期从原始全彩图像(如果你有的话)中得到的文件更
大而且质量会更低。为了解决这个问题,你需要在压缩之间“平滑”GIF图像。平滑将附近的像素平均,从而接近
你感觉看到的颜色,同时在这个过程中,摆脱给JPEG带来麻烦的颜色急剧变化。平滑的合理利用将会减少被压缩
文件的大小,同时给你一个更好看的输出图像。


    通过使用IJG JPEG软件(cjpeg或派生程序),一个简单的平滑能力是内置的。GIF转换时,尝试使用-smooth
10”左右。对于高质量的GIF文件,1025之间的数值看起来效果不错。重手抖动的GIF文件可能需要更大的平滑系数。
(如果你能看到恰好没有扩大的GIF图像中规则的精细尺度的patterns,那么就绝对需要很强的平滑能力。)过大的平
滑系数将会使输出图像变得模糊,这不是我们想看到的。如果你是一个图像处理向导,你也可以做一个添加单独过滤
程序的平滑处理,但是这些工具的合理使用超出了本FAQ的范围。


    假设你已经选择一个好的平滑系数,当转换彩色GIF图像时,85左右(比默认值稍高一点)的质量设置通常效果
很好。如果你使用一个合理的平滑系数却无法隐藏抖动因素,你可能会需要更高的质量设置。抖动严重的GIF图像最
好还是保留为GIF


    不要奢望转换自GIF图像的JPEG文件会与那些直接由全彩原型产生的JPEG文件一样小。抖动噪声会浪费空间,
但是没有必要平滑掉所有的没有使图像变得模糊的噪声。通常情况下,质量好的转换后的JPEG将会是GIF文件的1/2
1/3大小,而不是在Section 4里建议的1/4。如果转换后的JPEGGIF文件的一半还大得多,这就表示该图像根本
就不应该被转换,是个好兆头。


    这一切的要点是:“cjpegquality 85smooth 10”的设置有可能是转换彩色GIF图像的良好出发点。如果你很
在意图像。那么你将需要查看转换的结果,并且可能需要尝试一些其他的设置。盲目地使用一个或其他设置来转换
一个大的GIF库将会后患无穷。
努力打造安富莱高质量微信公众号:点击扫描图片关注
回复

使用道具 举报

0

主题

1

回帖

1

积分

新手上路

积分
1
发表于 2020-4-9 01:06:08 | 显示全部楼层
非常有趣的话题。 非常感谢您分享这些精彩的信息。 我将很高兴与我的朋友们分享。 祝一切顺利!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-11 16:55 , Processed in 0.226754 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表