JPEG

From Antalya
Jump to: navigation, search

Basics

The name stands for Joint Picture Experts Group, and essentially was developed to reduce the file size of a natural image, so that storage, and data transmission of these images can be reduced. JPEG uses a lossy compression (as opposed to PNG which is lossless). This means that the image will lose some of its quality so that the size can be reduced. This quality is actually a parameter and the user can find a trade-off between quality and the size of the image.

Size of an image

Each pixel of a digital picture is encoded using three values for Red, Green and Blue components. Typically 1 byte is used for this encoding, so that every pixel needs 3 bytes of information stored. For a 20 Megapixel image it is easy to see that we would need 60 MBytes of data. Some cameras have even better quality, and need to encode the color information with up to 6 bytes doubling the size of the image.

This size is not only a problem on for storage (occupying more space), but also for data transfer (takes longer to download, copy) and also processing (programs need more time to open, save files).

How does JPEG compress

Glad you asked. Without going too much into detail, JPEG compression takes advantage of the fact that natural images do not change their color values abruptly, but there is a rather gradual change. Instead of encoding the image directly with color values, JPEG takes small boxes (8 pixels by 8 pixels) and looks at a rate of change between adjacent pixel values (OK it is a bit more complicated, but this is not so far off). This ends up being a series of sums. The first elements express gradual change, and the last elements are for very sudden changes. What JPEG does is, depending on the quality drops the last elements from the sum series which represent sudden changes.

Since 'natural images do not change abruptly, dropping some of those elements will not make noticeable changes, but will reduce the amount of information needed. Of course if you compress too much, you will start noticing the change

An example

This picture has been scaled to 1280x853 from a uncompressed original of 20 Megapixel and then encoded using different quality factors.

The table below shows the size of the same picture with different JPEG quality settings, as well as how much the size shrinks. Note that even with very high quality settings (which would not be visible), the size of the picture shrinks by 5-10x. When compared to the raw bytes needed to encode the image (1280 x 853 x 3) a quality factor of 90% reduces the file size by a factor of more than 20x!

Quality Size (Bytes) Reduction in size
RAW image 3'275'520 no compression
100 861'928 baseline
90 162'325 5.3x
80 84'854 10.2x
70 66'593 12.9x
50 49'530 17.4x
25 33'696 25.6x
Do not use JPEG for artificial images

While JPEG is great for natural images that have gradual changes, some computer generated images can have very sharp changes in color/intensity. As JPEG compression reduces the capability to make these changes, when they are used for these images there are visible artifacts.

Worst case example

The following image was created using GIMP to mimic text written on a cardboard box. Normally for such text anti-aliasing is used that creates a smooth transition, but for this example this was deliberately not used creating a very sharp black and white transition. The original image is 5'568 x 3'712 pixels (corresponding to a 20 Megapixel resolution of the Nikon Z FC camera), a 256 x 170 pixel part was cropped and scaled up to 1280 x 850 pixels so that individual pixels show up well when magnified.

You can clearly see ghosts around the transitions that are sort of disturbing below 80% quality.

Notice that this is a very extreme example, for comparison, this is how the entire picture scaled down to 1280x853 pixels with the extreme quality factors.

What to do for these pictures

If you want to store artificial pictures, using a different format like PNG is the way to go. PNG will also compress pictures, but the compression will be lossless. It can not compress natural pictures as well as you can with JPEG, but on artificial pictures it will leave no artefacts and for pictures that have fewer colors and hard transitions, compress better than JPEG.

Your camera will have different JPEG quality settings

Although some like to argue otherwise (see JPEG vs RAW), JPEG is actually quite a sensible choice to store the pictures you make. Most cameras will allow you to select different JPEG quality factors. Unlike the above mentioned quality these will be adjusted according to the scene and will differ from camera to camera as well, so these settings do not correspond one to one to a given quality factor I used above. Nikon usually has three quality settings: Fine, Normal, Basic.

Worst case: the cardboard box

You can of course take pictures where there are artificially generated images (i.e. computer print-outs). The following is the real picture with (sort of) the text I used above. We have the original scaled down to 1280x853 pixels and a 256x170 pixel crop magnified 5x on the other side.

Now that you know where to look, you can see the artefacts from increased JPEG compression.

A more realistic picture

Same story, but this time a more natural picture. I am concentrating on the window blinds in the middle which actually has the most color change in the picture. While i the previous worst case picture you could make out a difference between Basic and Normal here it will be very hard to pin point it to JPEG artefacts. Note that these pictures were not taken at the same time, and there is some movement/change from shot to shot (as well as a skewed camera).


Finally our Bear

Here is a softer picture, the bear that we already used before. I am not adding a zoomed in version, as it is not possible to find a region that would highlight issues related to JPEG compression.

Final words

JPEG is a very useful file format for natural images. Cameras provide different quality factors, but for all but the most extreme cases, the Normal settings will produce pictures that will not have any visible artefacts, while significantly compressing the image. Make your own experiments to see what the trade-off is, however to see the artefacts you need to find pictures that will show these, so I hope these pages help.




These pages are for Amateur Photographers and not really for seasoned photographers and professionals. I have no affiliation or commercial interest with any brand/make. I write from my own experience. I ended up using mainly Nikon, so I am more familiar with this brand than others. See price for notes on pricing as well as photography related links.