Image compression (source code)

= Image compression
{wiki}

<Image compression> applies <data compression> to pictures, usually represented by <raster images>. Nearby <pixels> often have related shades; an encoder can exploit that structure. A flat region invites <run-length encoding>, while <lossy block averaging> trades fine detail for fewer stored samples. The choice depends on whether exact <pixel> values matter and on the patterns in the image.

<Lossless source coding> recovers the encoded <pixel> values exactly, including sharp text or numerical image data. <Lossy compression> preserves an approximation and can remove features that later <image processing> might need. A visually smooth result is therefore a different promise from exact recovery. Neither method has a universal compression ratio: the chosen <file format>, its <file header> and the particular image all matter.

<PNG> is a real format with <lossless source coding> of its encoded image data. Its specified compression uses <Deflate> on reversibly filtered rows, rather than our toy <RLE> format. The <lossy block averaging> example is also a teaching format, not a description of <PNG>. A lossy preprocessing step followed by saving as <PNG> preserves the already changed <pixels>, not the earlier original.<Portable Network Graphics (PNG) Specification (Third Edition) by Mark Adler and others>[W3C, PNG Specification, Third Edition (2025), section 10, Compression]{ref}