Search the FAQ Archives

3 - A - B - C - D - E - F - G - H - I - J - K - L - M
N - O - P - Q - R - S - T - U - V - W - X - Y - Z - Internet FAQ Archives

JPEG image compression FAQ, part 1/2
Section - [9] What are some rules of thumb for converting GIF images to JPEG?

( Part1 - Part2 - Single Page )
[ Usenet FAQs | Web FAQs | Documents | RFC Index | Forum ]

Top Document: JPEG image compression FAQ, part 1/2
Previous Document: [8] What is color quantization?
Next Document: [10] Does loss accumulate with repeated compression/decompression?
See reader questions & answers on this topic! - Help others by sharing your knowledge
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.

User Contributions:

Report this comment as inappropriate
Nov 11, 2018 @ 11:11 am
Artikel ini menjawab Pertanyaan yang Sering Diajukan tentang kompresi gambar JPEG.
Ini adalah bagian 1, yang meliputi pertanyaan umum dan jawaban tentang JPEG. Bagian 2
memberikan petunjuk khusus sistem dan rekomendasi program. Seperti biasa,
saran untuk peningkatan FAQ ini disambut baik.

Baru sejak versi 14 Maret 1999:
* Memperluas item 10 untuk mendiskusikan rotasi tanpa loss dan pemangkasan JPEG.

Artikel ini mencakup bagian-bagian berikut:

Pertanyaan dasar:

[1] Apa itu JPEG?
[2] Mengapa menggunakan JPEG?
[3] Kapan saya harus menggunakan JPEG, dan kapan saya harus tetap menggunakan GIF?
[4] Seberapa baik gambar kompresi JPEG?
[5] Pengaturan "kualitas" apa yang bagus untuk JPEG?
[6] Di mana saya bisa mendapatkan perangkat lunak JPEG?
[7] Bagaimana cara melihat gambar JPEG yang diposkan di Usenet?

Pertanyaan lanjutan lainnya:

[8] Apa itu kuantisasi warna?
[9] Apa beberapa aturan praktis untuk mengkonversi gambar GIF ke JPEG?
[10] Apakah kehilangan terakumulasi dengan kompresi / dekompresi berulang?
[11] Apa itu JPEG progresif?
[12] Dapatkah saya membuat JPEG transparan?
[13] Apakah tidak ada JPEG lossless?
[14] Mengapa semua argumen tentang format file?
[15] Bagaimana saya mengenali format file yang saya miliki, dan apa yang harus saya lakukan?
[16] Masalah kompatibilitas umum apa saja yang ada di sana?
[17] Bagaimana cara kerja JPEG?
[18] Bagaimana dengan pengkodean aritmatika?
[19] Mungkinkah FPU mempercepat JPEG? Bagaimana dengan chip DSP?
[20] Apakah tidak ada standar M-JPEG untuk film?
[21] Bagaimana jika saya membutuhkan lebih dari 8-bit presisi?
[22] Bagaimana program saya mengekstrak dimensi gambar dari file JPEG?


[23] Di mana saya bisa belajar tentang menggunakan gambar di World Wide Web?
[24] Di mana daftar FAQ diarsipkan?

Artikel ini dan rekannya diposting setiap 2 minggu. Jika Anda tidak dapat menemukannya
Bagian 2, Anda bisa mendapatkannya dari arsip di
(lihat "[24] Di mana daftar FAQ diarsipkan?"). Bagian 2 sangat sering berubah;
dapatkan salinan baru jika yang Anda baca berusia lebih dari beberapa bulan.

Comment about this article, ask questions, or add new information about this topic:


Top Document: JPEG image compression FAQ, part 1/2
Previous Document: [8] What is color quantization?
Next Document: [10] Does loss accumulate with repeated compression/decompression?

Part1 - Part2 - Single Page

[ Usenet FAQs | Web FAQs | Documents | RFC Index ]

Send corrections/additions to the FAQ Maintainer:

Last Update March 27 2014 @ 02:11 PM