What are jpeg artifacts and what can be done about them?
I know JPEG is a "lossy" compression algorithm that discards information in order to save space. What is the visual impact of this? I've heard of "JPEG artifacts". What do these look like?
Are there situations where the same level of compression creates more artifacts and looks worse? Does the content of the image matter at all? What does the algorithm handle well, and what does it have trouble with?
Assuming JPEG is a requirement, is there a way to reduce artifacts? If I'm uploading to a web service which will apply its own compression outside of my control, is there anything I can do to the image in advance to make it survive this better?
Some answers for pieces of this question exist here: http://photo.stackexchange.com/questions/5317/how-to-avoid-jpeg-compression-artefact-for-web-resolution
Thanks @dpollit. I didn't see that one due to the British spelling and lack of tags. Plus, it's is very specifically about low-resolution and low file size.
I would direct you to my answer on the topic @dpollit linked before, specifically the web site linked from it, which has a whole load of sample images from every major "quality" level, with pixel-level detail, for a variety of types of images.
I've noticed when I shot a picture of the moon and exported the file to JPEG the file is relatively small since a large part of the picture is black, exposing for the moon. The compression in this case seems to work best since it's able to effectively through out all the other black redundant information and worry about the moon.
@mattdm I'd just like to point out that "What does the algorithm handle well, and what does it have trouble with?" doesn't apply to every .jpeg. Different cameras and software (say, converting a RAW to .jpeg) handle it differently which is why different cameras yield different results. So you may not get a definitive answer.
+1 Because I'd like to know as well. It's just that, I guess it's hard to know what exactly the camera is doing without the source code, therefore no references. But hopefully someone can answer well enough.
@BBking: here, though, I'm not talking about RAW→JPEG conversion as a whole, just the artifacts introduced by compression. Since that compression is standard, I don't think there's actually much room for variation here. (Other than using `ISLOW`, `IFAST`, or `FLOAT` for quantization.)
Sure. The only reason I brought up RAW was because off you have a computer program, you may be able to backwards engineer the process. Also, with regards to web services compressing images, that's more a resizing compression. Once it's at a, say 500px x 750px size, there's no real need to compress it to a more lossy image to save a couple of kbs. Still a validquestion though. :-)
@mattdm Sorry, I forgot to ask. Is there a particular web service/site you had in mind or just in general?
Using the current photo of the week image. This is the high-quality JPEG:
re-saved in Gimp with JPEG quality 80 (low); please note the general loss of sharpness, "dots" around high-contrast edges, loss of detail in low-contrast areas:
and re-saved in GIMP with JPEG quality 30 (very low); please note evident 8x8 blocks and severe loss of sharpness and color detail:
Three kinds of distortions
JPEG tends to introduce two three kinds of distortions:
- general loss of sharpness and oscillations around high-contrast edges: these are due to approximating intensity transitions with smooth functions (cosines); you see them as small "dots" or "halos" around the edges; they are particularly easy to see in the images of text of hand-drawings.
- blocking structure: image is processed separately for every 8x8 block (or bigger in case of chroma downsampling), block edges become visible at high compression ratios.
- loss of color detail: it depends on saving parameters, the program may aggressively "downsample" (reduce resolution of) chromaticity channels; it is rarely an issue for the natural photography.
Visible block structure and halos around edges are usually referred to as JPEG artifacts. Let's zoom in our example to see them better. From left to right, a crop from the original, JPEG Q80 and JPEG Q30 images. I marked artifacts in green (circles for halos, and dots for 8x8 blocks):
As any information loss, you cannot actually recover it. Sharpening may help to recover lost edge contrast, but makes "halos" more evident; denoising may help to remove "halos", but reduces sharpness even further. If block structure is visible, it is probably too late. Just keep the original high-resolution, high-quality images around, and don't overwrite them.
If you control JPEG compression parameters and want to maximize image quality:
- keep the compression ratio as low as you can (use high-quality settings)
- consider downsampling chromaticity channels (it may be almost unnoticeable for some images, and allows for lower compression ratio in the luminosity channel given the same file size constraint)
- consider using floating-point discrete cosine transform (it may increase precision of the transform, but file saving will take longer)
- consider using lower resolution instead of higher compression ratio (given the same bound on the file size)
If you upload to a third-party service, and don't control compression parameters, you cannot do much about it:
- choose a service which is known to prefer high-quality JPEG (Flickr, SmugMug, 23hq, 500px) over services which are known to over-compress to save traffic (Picasa, Imgur, Dropbox); usually you get what you pay for.
- try resizing photos yourself and uploading the right size (some services will re-compress it anyway, some may serve your file as uploaded)