In this article we will be comparing the different compression methods available to use for WebP images. To do this, we will be using our Java image library, JDeli. We will be comparing file size, quality of output, and speed.
For the baseline of our comparisons, we will use the following uncompressed image. It has a file size of 180KB. It takes just under 3 seconds to encode.
What types of compression does WebP support?
Unlike most other image formats, WebP has both a lossless and lossy compression format. Which is best for you all depends…
What is Lossy compression?
Lossy compression is known as the irreversible compression due to the fact some data is removed. The process uses the VP8 prediction code commonly used for videos which includes the algorithm DCT (Discrete cosine transform) to then be able to remove redundant data. This does mean quality loss can sometimes be expected but is usually not visible, also something to bear in mind you cannot return to the original quality once you have applied this compression. The advantage is that this compression can reduce the file size much more effectively.
Using lossy compression reduced the file size to 6KB and took just under 2 seconds, and the quality was set to a default of 75.
What is Lossless compression?
This compression unlike lossy, preservers the original data so when decoded can return the file to its original form. To achieve this, multiple transformations are made including several color transforms along with a spatial transform and then finished off by applying a form of entropy coding known as LZ77 huffman coding. This does then mean that the reduction in file size won’t be as significant but that is not always the most important aspect when compressing.
Using lossless compression reduced the file size to 67KB and also took just under 2 seconds.
As we can see the lossy far out ranks the lossless compression due to the much smaller file size for similar quality. If file size is your main focus lossy would be your first choice but if you want to be able to return to the original image than the hit of an extra 61KB wouldn’t matter as much especially when they both take the around the same time to complete.
Start reading and writing images with one line of code
BufferedImage image = JDeli.read(streamOrFile);
JDeli.write(myBufferedImage, OutputFormat.HEIC, outputStreamOrFile)