Images are one of the most commonly used digital assets in fields such as communication, business, and so on. Compression refers to the process of reducing the size of digital assets, such as images, audio, and video, without a significant effect on their quality. There are two basic types of compression, namely lossy compression and lossless compression.
Our focus in this article, however, is on lossless compression, particularly in image compression. We’ll also explain how you can use Cloudinary, a cloud-based media management platform, to apply lossless compression to your media assets efficiently.
In this article:
- What is Lossless Compression?
- How Lossless Compression Works
- Lossless Image Compression In the Real World
- Lossless Compression Using Cloudinary
What is Lossless Compression?
Lossless compression is a compression technique that reduces file size without losing any data, and the file can also be decompressed back to its original state with no loss in quality.
Some common examples of lossless compression file formats include:
- Images: Portable Network Graphics (PNG), Graphics Interchange Format (GIF), Bitmap (BMP), Tagged Image File Format (TIFF)
- Audio: Free Lossless Audio Codec (FLAC), Apple Lossless Audio Codec (ALAC)
- Video: Motion JPEG, FFmpeg Video Codec 1 (FFV1)
- Documents: ZIP, GZIP
How Lossless Compression Works
A typical lossless compression algorithm works by finding redundant or repetitive patterns within a file and encoding them in a more efficient manner. The three most commonly used algorithms are discussed below.
Run-length encoding (RLE)
RLE is one of the most commonly used lossless compression algorithms for images with repeated data (also called runs), such as icons, logos, animation, etc. RLE works by converting the input data (e.g., image file) into strings of character. The algorithm then compresses the data by reducing the physical size of the repeating string of characters by identifying and counting consecutive occurrences of each character.
For example, suppose we want to compress the following icon made up of black and white colors:
The RLE algorithm will encode the image data by converting it to a string of characters, such as:
WWBBBBBWWWWWWWWWBBWWWWWWWBBBWW WWWWWWBBBWWWWWWWBBWWWWBBBWWWWW WWWWWWWWWWWWBBBBWWWWWWWWWBBBBB WWWWWWWWWWWWWWWWWWWWWBBBBBBBBB ... ...
Now, let’s imagine we need to convert this data into human readable language so other people can decode it. Instead of writing out all the characters, the algorithm will compact the characters in each row into a sequence like: 2W 5B 9W 2B 7W 3B 2W and so on. This saves a lot of computation time and makes saving the data more efficient.
Huffman Coding Algorithm
Huffman coding is a foundational algorithm for lossless data compression, providing a highly efficient way to reduce file size without losing any data. The algorithm compresses data, like text, images, and videos, by reducing the number of bits necessary to represent the most frequently occurring characters or symbols in a dataset. Shorter codes are assigned to frequently used symbols and longer codes to less frequently used symbols, resulting in an efficient compression of the dataset.
The goal of the Huffman coding algorithm is to find a prefix-free binary code (a set of binary codewords with the minimum expected codeword length) given a set of symbols and their associated weights (which are usually proportional to their probabilities of occurrence).
A Huffman tree generated from the exact frequencies of the text “this is an example of a huffman tree”. Source
One advantage of Huffman coding compression algorithm is its adaptability in handling any kind of input, whether it is text, images, or any other data. However, Huffman coding is not always the best for very short data because of the overhead of building a Huffman binary code tree, which can be computationally expensive for real-time applications.
Lempel–Ziv–Welch (LZW) Compression Algorithm
Lempel–Ziv–Welch (LZW) is a universal lossless data compression algorithm created by Abraham Lempel, Jacob Ziv, and Terry Welch. LZW compresses data by identifying repeated sequences of data (patterns) and encoding them as shorter symbols. It achieves this by building a dictionary of strings dynamically during compression and using references to these strings in place of the original data. The LZW compression algorithm is commonly used in compressing GIF, TIFF and PDF files.
While the LZW algorithm is very efficient for data that contains repeated patterns, it’s not ideal for data that doesn’t have such patterns (e.g., already compressed data or random data).
Lossless Image Compression in the Real World
Lossless image compression finds applications in various fields, a few of which are outlined below.
- Web development: Web page and application load speed has a huge impact on search engine optimization, user’s experience and other performance metrics. Developers use several techniques to optimize web performance and image compression is one of the commonly used methods. It’s sometimes used together with other methods, such as caching, lazy loading, load balancers, and CDNs for optimal performance.
- Photography and graphic design: Photographers and graphic designers often work with images through multiple stages of editing, which involves tasks like color correction, adding effects, and so on. Lossless compression allows them to compress images without affecting their layers or color profiles, enabling preservation of details for further editing.
- Medical imaging: Lossless compression is essential in storing patient records and data from procedures like MRI or CT scans. Lossless compression ensures that the details in the captured images are preserved, allowing medical professionals to make accurate diagnoses using the best quality data. Choosing the right compression method is important as lossy compression might remove minor details that could be critical in identifying conditions or abnormalities.
- Digital archiving: One of the key advantages of lossless compression is that it preserves the exact quality of the original data, making it ideal for archiving data. In government and legal institutions, museums and libraries, digital archiving of photographs, artworks and other documents often involves converting physical assets into digital formats. Lossless compression ensures that important documents and historical artifacts are preserved without losing any details, which is critical for maintaining the integrity of the originals for future generations.
Advantages and Disadvantages of Lossless Image Compression
Some benefits of lossless image compression include:
- Fast and efficient: Lossless image compression reduces file size quickly, saving space and speeding up the process of sending or storing images without losing any details.
- Image quality is preserved: Lossless compression makes the image look exactly the same as the original after compression, making it perfect for work where high-quality images are needed, like in photography.
- Compressed images can be restored: Unlike lossy compression, where compression is permanent, you can get the original image back from the compressed version without losing any information or quality with lossless compression.
Some common disadvantages include:
- Limited Reduction in File Size: Lossless compression doesn’t reduce file size as much as other methods because it keeps all the original data.
- Works for limited data types: Lossless compression is only effective for specific types of data, like text or simple images with repeated patterns. For other data, like complex images or video, it might not reduce the file size as dramatically as lossy compression.
Lossless Compression Using Cloudinary
Cloudinary is a cloud-based media management solution that offers a wide range of features for managing, optimizing, and delivering media assets. One of its key offerings is the ability to perform lossless compression on media assets without compromising the quality of the original file.
Cloudinary also supports several lossless media formats like PNG, GIF, and WebP, and provides transformation parameters that enable you to apply lossless compression efficiently.
The easiest way to apply lossless compression to your image files is to add the
q_auto
transformation parameter to the images URL. Theq_auto
parameter tells Cloudinary to use its intelligent quality and encoding algorithm to analyze the image to find the best quality compression level and optimal encoding settings based on the image content and the viewing browser.If you also want the image to be returned in any of the supported lossless file formats, you can add the
f_<supported format>
transformation parameter to the URL.For example, to return the compressed image in PNG format, we can use the
f_png
parameter as follows:Tip: You can also use the manual browser-based compression tool by Cloudinary to compress your images if you’re looking for a fast non-programmatic way to optimize your images.
Conclusion
In conclusion, lossless compression is a widely used technique in fields where the highest level of accuracy and data preservation is essential. Whether in photography, medical imaging, or digital archiving, lossless compression ensures that critical details remain intact, allowing for future editing, analysis, or display with no quality degradation.
Transform and optimize your images and videos effortlessly with Cloudinary’s cloud-based solutions. Sign up for free today!