Reduce image file size
An uncompressed 4K image contains over 8 million pixels. Storing the exact RGB value for every single pixel takes about 25 Megabytes of data. Compression reduces this massive footprint using one of two strategies: throwing data away (Lossy) or finding patterns (Lossless).
Lossy algorithms exploit human biology. The human eye is much more sensitive to changes in brightness (luma) than it is to color (chroma).
Algorithms like JPEG use Chroma Subsampling to average out the color of neighboring pixels, effectively throwing away color data you wouldn't notice anyway. It then uses the Discrete Cosine Transform (DCT) to group pixels into 8x8 blocks of frequency patterns.
Lossless compression never throws away visual data. Every pixel is perfectly reconstructed. Instead, it uses mathematical techniques (like Deflate or LZ77) to find repeating patterns.
If an image has a completely white background with 1,000 identical white pixels in a row, the algorithm just writes "repeat white 1,000 times" to the disk instead of individually saving the RGB values for all 1,000 pixels.
Why do JPEG images often look blocky or artifacted when heavily compressed?