What Is Image Compression?
If you're interested in photography, or have a website, you might have heard about image compression. You may have even asked yourself questions such as, "What is image compression?" or "How does image compression work?". This post is all about explaining various image compression methods to help you come to a decision regarding which compression method is best suited for your particular use case.
What is image compression?
To help answer the question of "what is image compression", first, let's look at the definition.
Image compression is an application of data compression that encodes the original image with few bits. The objective of image compression is to reduce the redundancy of the image and to store or transmit data in an efficient form.
At its core, image compression is when you remove or group together certain parts of an image file in order to reduce its size. Why do that? Here are a few reasons.
- For website optimization. Sites with uncompressed images can take longer to load, and can cause your visitors to bounce because of this.
- For sending and uploading images. Uploading an uncompressed image can take a while, and some email servers have a file size limit.
- For reducing the storage impact on your hard drive.
How does image compression work?
There are two kinds of image compression methods - lossless vs lossy. Let's take a quick look at them both.
Lossless compression is a method used to reduce the size of a file while maintaining the same quality as before it was compressed. For example, in a DSLR camera, you probably have the option to save photos as either RAW or JPEG. RAW files have no compression and are great if you're a professional photo editor. But they take up more space. JPEG, on the other hand, won't fill up your hard drive as fast, but some of the data is lost in the conversion.
Types of lossless images include:
- RAW - Found in many DSLRs, and keeps all the light data received from the camera's sensor. For a professional, this great news. However, these files types tend to be quite large in size. Additionally, there are different versions of RAW, and you may need certain software to edit the files.
- PNG - Compresses images to keep their small size by looking for patterns on a photo, and compressing them together. The compression is reversible, so once you open a PNG file, the image recovers exactly.
- BMP - A format found exclusively to Microsoft. It's lossless, but not frequently used.
It should also be noted that converting a lossy photo back to lossless won't restore the photo's data.
In order to give the photo an even smaller size, lossy compression discards some parts of a photo. However, this doesn't mean the photo will look bad. Here are the two main types of lossy compression.
Also known as JPEG, this format gets rid of bits and pieces of a photo that you may notice depending upon the level of compression you apply. A normal amount of compression will not be noticeable, while extreme compression may be obvious.
There are also other ways a JPG image's quality may be reduced. If you rotate the JPG too much, you'll notice a difference in quality. This is because the photo has to recompress itself with every rotation, losing some data in the process. There are however programs out there that rotate a JPG losslessly. The same degradation applies if you save a JPG multiple times.
GIF compresses files by reducing the number of colors it has. If the photo has more than 256 colors (the maximum amount of colors older computers could have) this format will make the image look less appealing. The best use for GIFs are for images that are animated.
The example below shows a comparison between GIF images which range from 8 colors to 256 colors.
Methods of compression
Now that we've discussed various image formats, the following explains a few image compression methods used to achieve either lossless or lossy compression. These algorithms, or variations of these algorithms, are also what is used in image compression tools and services.
Deflate is a lossless data compression algorithm used for PNG images. It uses a combination of LZ77 and Huffman coding to achieve compression results that do not affect the quality of the image.
Run-length encoding is a form of lossless compression that takes redundant strings or runs of data and stores them as one unit. Say you have a picture of red and white stripes, and there are 12 white pixels and 12 red pixels. Normally, the data for it would be written as WWWWWWWWWWWWRRRRRRRRRRRR, with W representing the white pixel and R the red pixel. Run length would put the data as 12W and 12R. Much smaller and simpler while still keeping the data unaltered.
Transform encoding is a lossy compression commonly used for JPEGs. There are millions of shades of colors, and transform encoding takes colors that have similar shades and makes them one single value. Depending upon the compression value you define (i.e. the number of shades of colors you group together) you may or may not notice a difference in the image's quality.
Hopefully, this post helps answer the question "what is image compression?" and "how does image compression work?". As you can see, there are many moving parts. No matter how fast the Internet gets or how performant storage becomes, there will always be room for compression. Image compression is useful for a variety of reasons and it is dependent upon the image size reduction you aim to achieve along with the quality level you plan to keep that will determine which form of compression you should use.