Lossy and Lossless Image Compression: JPEG vs. PNG
When compressing an image, we either want to temporarily reduce its size (such as when sending work files via email) or permanently shrink its size (such as when displaying images on the internet).
Lossless compression techniques are developed to temporarily and reversibly reduce information. As the name suggests, they do not lose any data during compression. An example of lossless compression in text is the use of abbreviations. People know that "open source" expands into "open-source software," so you can replace the five-character phrase with just two characters in an article.
In image formats, a similar compression method is "indexed color." First, all the colors in an image are listed, and each color is assigned a unique index number. When describing the color of a pixel, we only need to provide its index instead of repeatedly specifying the exact color values.
Lossy compression, on the other hand, is designed to permanently and irreversibly reduce image size. This is especially useful for final images displayed on websites, as they are not intended for further editing. By leveraging psychology and statistical methods, lossy compression selectively discards image data that has minimal impact on human perception.
One fundamental process in JPEG compression is chroma subsampling, where image data is split into a luminance (brightness) channel and two chroma (color difference) channels (red-green contrast and blue-yellow contrast). JPEG then compresses only the two chroma channels, as human vision is much more sensitive to brightness changes than to hue and saturation variations.
JPEG and other lossy formats also use cosine waves to represent light and dark variations in an image. As a result, they excel at compressing gradual color transitions but struggle with sharp color contrasts.
Lossless image compression works in the opposite way—it is better suited for high-contrast images with fewer colors, but it does not compress images with smooth color gradients as efficiently.
Another key difference between lossy and lossless formats is that lossy formats degrade image quality with each re-encoding, while lossless formats ensure that the image quality remains the same after each encoding. If you open a JPEG file in Krita, make minor edits, save it, and repeat the process several times, you will notice a gradual decline in image quality. This is an inherent characteristic of lossy image compression, which is why we should always use a lossless working file format when editing images.