How does JPG work?
How the JPG format works is more background knowledge. You can move on directly to sample images if you are interested only in practical results.
The process of JPG coding is mathematically complex, among other steps it contains:
- A conversion of color space changes the representation of each pixel by three values from 0 to 255 for the three basic colors red, green and blue (RGB color space) into another one consisting of one value for the brightness (luminance) and two for the color (chrominance, the YCrCv color space) – a standard that is also used for video signals.
Because the human eye is more sensitive to differences in brightness than in color, the color signal can be stored with reduced resolution which leads to reduction of data.
Some image processing programs offer a setting „sub sampling“ for this. Simply speaking it means that two or even four pixels next to each other et the same color – but not the same brightness that is stored in full resolution.
- A division into blocks of 8×8 pixels before the actual compression is done – for this reason JPGs of low quality show small square sized patterns.
- The actual compression that reduces the image data into a smaller amount of data and that can be reversed only approximately. The level of compression can be varied.
The loss in quality occurs with each compression and decompression, i.e. it cumulates if you save and re-open a file repeatedly, even if you save it again without changing it.
This is the mathematically most challenging part, a so called discrete cosine transform, a variant of the Fourier transform. You need to study mathematics to really grasp the necessary knowledge.