High-quality compression & format conversion made simple!

Several commonly used image compression algorithms

Image compression is the process of reducing the file size while retaining the same or most of the data. The principle is to eliminate unnecessary data or reorganize the data in a more efficient format. When we perform compression, you can choose between lossy compression and lossless compression. Lossy compression permanently erases some data and cannot be restored. Lossless compression, on the other hand, can ensure all the original data. The method used in the compression process depends on how much accuracy you want to maintain for your file.


This article will introduce several compression methods based on lossless and lossy compression.


I. Lossless Compression

Lossless compression can precisely restore the original image without any information loss, making it suitable for scenarios with extremely high requirements for image accuracy.


1. Run - Length Encoding

-Principle: For continuously repeated pixel values (or data), it is represented in the form of "value + number of repetitions". For example, if a row of pixels in an image is [255, 255, 255, 100, 100, 50], it can be encoded as (255,3), (100,2), (50,1).

Application: The lossless compression mode of the BMP format, fax images.

Applicable Scenarios: There are a large number of continuous identical pixels in the image, but the compression ratio for complex images is relatively low.


2. Huffman Encoding

Principle: Unequal - length codes are assigned according to the probability of pixel values appearing. Pixels with high frequency of occurrence are encoded with short codes, and those with low frequency are encoded with long codes. The total data volume is reduced through probability optimization.

Characteristics: There is no fixed code table. It is necessary to first count the probability distribution of pixels in the image and generate a specific code table.

Application: The lossless mode of JPEG, the PNG format, and image processing in ZIP compression.


3. LZW Encoding

Principle: A dictionary is constructed to store repeated pixel sequences (not single pixels). The sequence is replaced with a dictionary index, and the dictionary is dynamically updated to adapt to the characteristics of the image.

Characteristics: High compression efficiency, no need to pre - calculate probabilities in advance, but the dictionary will add a small amount of extra storage.

Application: The GIF format, some compression modes of TIFF, and image compression in PDF.


4. Predictive Encoding

Principle: Utilize the correlation of adjacent pixels (such as the left - hand or upper - hand pixels) to predict the current pixel value, and only store the prediction error (the error is usually small and easy to compress).

Typical Method Diffe:rential Pulse Code Modulation, often used in combination with Huffman encoding.

Application: JPEG lossless compression, medical images.


II. Lossy Compression

Lossy compression improves the compression ratio by discarding some information that is not sensitive to the human eye (such as high - frequency details) and cannot completely restore the original image. It is suitable for scenarios where high visual effects are required but some accuracy can be compromised (such as web images, video frames, etc.).


1. Transform Coding

Principle: The image is transformed from the spatial domain (pixel values) to the frequency domain (such as Fourier transform, discrete cosine transform). The low - frequency components (determining the overall brightness) are retained, and the high - frequency components (determining the details) are discarded. Then, the image is reconstructed through inverse transformation.

Typical Method: Discrete Cosine Transform (DCT), which is the core step of JPEG compression:

Divide the image into 8×8 pixel blocks.

Perform DCT transformation on each block to obtain frequency coefficients.

Quantize the high - frequency coefficients (approximate processing) to reduce the data volume.

Perform entropy encoding (such as Huffman encoding) on the quantized results.

Application: JPEG, JPEG 2000 (improved to wavelet transform), MPEG video frame compression.


2. Wavelet Transform

Principle: Compared with DCT, the wavelet transform can capture both the low - frequency and high - frequency information of the image simultaneously and decompose it at different resolutions (multi - scale analysis). After compression, the details are better retained.

Characteristics: The compression efficiency is higher than that of DCT, and it has strong noise resistance, making it suitable for large - size images.

Application: JPEG 2000, medical images (such as efficient compression of CT and MRI), remote - sensing images.


3. Fractal Compression

Principle: Utilize the self - similarity of the image (similar structures between the local and the whole). Describe the repeated patterns using a fractal mathematical model, and reconstruct the image through iteration without storing the complete pixel information.

Characteristics: Extremely high compression ratio, especially suitable for images with self - similar features such as natural landscapes, but the encoding process is complex (time - consuming).

Application: Some image libraries, high - resolution image storage.


4. Compression Based on Visual Characteristics

Principle: Combine the characteristics of the human visual system (HVS). Retain more details for sensitive information (such as brightness) and perform more significant compression on insensitive information (such as chromaticity) (for example, in the YUV color space, down - sample the UV components).

Application: JPEG (using the YCrCb color space, compressing the Cr and Cb components), video coding (such as chroma subsampling in H.264/HEVC).


III. Hybrid Compression Methods

In practice, most image formats combine multiple methods to improve the effect. For example:

PNG: It uses lossless compression and combines predictive encoding (filtering), LZW encoding, and Huffman encoding.

WebP: It supports both lossy (intra - frame compression based on the VP8 video encoding) and lossless modes, balancing the compression ratio and quality, and is often used for web image optimization.


Summary

Lossless Compression: Suitable for scenarios that require precise restoration. Typical methods include RLE, Huffman encoding, LZW, and predictive encoding.

Lossy Compression: Suitable for scenarios that pursue a high compression ratio. The core methods are transform coding (DCT, wavelet transform) and visual characteristic optimization.