It is essential to understand the lossless and lossy image compression concept.
Image compression is an important process of image management and optimization in the case of huge image files. It assists in diminishing sizes of files, images may be stored, propagated and loaded without a lot of rigor while retaining reasonable quality. There are two primary types of image compression techniques - lossless and lossy. This article explores some of the techniques as well as explore on tools like Squoosh.
What Is Lossless Compression?
Lossless compression reduces the file size of an image to some degree without discarding any data. It also means that the source image can easily be reproduced from the compressed image file to some extent. This approach is particularly important for those applications where true image reproduction is desired, such as in medical imaging or for archival use.
Let's understand some of the techniques in Lossless Compression.
Run-Length Encoding (RLE): It is a lossless compression method that runs sequences of independent pixels by saving the pixel value and the pixel count. So something like "AAAABBB" will be compressed down to "4A3B".
Huffman Coding: Utilizes variable length coding with a prefix tree to assign shorter codes for frequent pixel values.
Palette Optimization: Another smart greedy approach algorithm that reduces the number of colors by creating an optimized palette, which in turn can go a long way in reducing the size of the file while maintaining a good trade-off for visual quality.
Remove Metadata: This cleans the image file of all the excessive info, such as camera settings or geolocation tags. This helps reduce file size, as well as protect privacy.
Delta Encoding: Instead of storing the pixel values, it stores only the differences between sequential pixels, which is useful for compressing gradients or patterns.
Common used formats for lossless compression include - PNG, GIF,
WebP (Lossless Mode).
What Is Lossy Compression?
Lossy compression shrinks the file size by removing some data, resulting in loss of quality that is not visible to the human eye. It’s perfect for web images and media streaming where smaller files are essential.
The below listed are some of the techniques for lossy image compression.
Discrete Cosine Transform (DCT): Converts image data to frequency components and removes less visible high-frequency components.
Quantization: Clusters and replaces similar colors with a value for that color, drastically reducing size.
Progressive JPEG: A format of images that loads them from low quality to high quality, in turn making the perceived load times faster.
Common used formats for lossy compression include - JPEG, WebP (Lossy mode), HEIC/HEIF.
Squoosh: The All-In-One Image Compression Tool
Squoosh is an open-source tool for both lossless and lossy compression, known for its ease of use and advanced features.
Visual Compare: Inspect the side by side before and after compression.
Color Palette Optimization: Auto optimize color palettes for PNG and WebP.
Convert Format: Convert images to modern formats such as WebP, AVIF, and MozJPEG.
Advanced Settings: Allows tweaking of compression levels, chroma subsampling and quantization
Metadata Removal: Remove metadata (just if really necessary to reduce file size).
Conclusion
Lossless and lossy compression have their own use case advantages. Lossless compression is more suitable for use cases including archiving quality images, better for preserving transparency (PNG files), and will be more favorable for image editing where the same image changing multiple times without losing data, while lossy compression on the other hand is more appropriate for use cases such as online image sharing, thumbnails, reducing web application footprint, or optimizing large image galleries for quicker load times.
Top comments (0)