- Basic breakdown of different types of compression
The Whole Picture:
If you’ve ever worked with images (either by sending digital photos, or building anything with a graphical component) you’ve probably encountered some of the major image file formats such as GIF, JPEG (jpg) or PNG. Those are some of the most common ones, but there are probably a few dozen different image file formats. They are all a little different and what differentiates them is compression and quality.
A digital image is essentially a list of colors. Each color maps to a pixel, and when those pixels are put next to each other, they comprise the image you see. Since today’s cameras (even on your smartphone) take pictures in megapixels (the latest iPhone takes pictures that contain 8.7 million pixels). If you wanted a rich set of colors, such as 16-bit color (about 32,000 unique shades), you’d end up with a 16 megabyte image for every picture. That would be unnecessarily huge.
(If you want to know which image file format is best to use, check this out)
Now we get to compression. So much data is redundant. For example, instead of the full 32,000 colors, you could limit the amount of color choices and greatly reduce the file size. This is obvious – less options, less size.
Then, we get a little more thrifty. Since frequently, two pixels that are next to each other may be the same color (or very very similar) you could simply combine them into one pixel. Now, if you had 3 or 4 in a row, you could just say “repeat this color 4 times” and use 30% of the space that repeating the color 4 times would use. If your image frequently re-uses the same color, this could lower the file size from 16 megabytes down to 3 or 4. At this point, this is where “lossy” and “lossless” compression come in. Lossless would require that the two pixels be exactly the same color (hence no actual information was lost). Lossy would say “as long as the colors are close.” Most of the internet uses lossy compression for images. It is much more efficient at reducing file size, yet it produces a bit of a blurring effect.
The last way to compress an image is to create an index. For this method, a program analyzes the image and comes up with a set number of colors – usually 8, 16, or 64 (although any multiple of 2 is possible). It scans the image and assigns every pixel to be the closest match to the 256 colors it chose. This allows the image to remain fairly crisp, but reduces the color range greatly.
The final isn’t really compression, but it’s about reducing file size. Often a 10+ megapixel image is simply gigantic (3000+px by 2000+px). This is extremely large and more than what most monitors and screens can even display, so what you see is the image shrunk down. By simply reducing the file size (although always try to do it by a factor of 4), you can reduce the file size greatly without losing much quality (although you lose the ability to zoom in with high quality).