Java WebP Image Support – Read and Save WebP Images

Java Webp Tutorial

WebP is a new generation image format developed by Google exclusively for the Web. It has both lossy and lossless compression support. When compared to the commonly used formats like JPEG, WebP has 25%-34% lesser file size for the same quality. WebP provides much better compression and reduced file sizes, which are crucial for the web since every byte of data saved matters.

In this tutorial, let’s see how we can process WebP images in the Java program. We will learn how to read and save/encode/write WebP images.

Add support for WebP into your Java program

As of writing this article in 2022, Java doesn’t natively support WebP images. However, there is a there is a library called webp-imageio that can support WebP image-io for Java. Let’s make use of this web-imageio library to process WebP images.

Add the dependency on to your project.

If you are using the Gradle, add the dependency as follows.

// https://mvnrepository.com/artifact/org.sejda.imageio/webp-imageio
implementation group: 'org.sejda.imageio', name: 'webp-imageio', version: '0.1.6'

If you are using the Maven, add the dependency as follows.

<!-- https://mvnrepository.com/artifact/org.sejda.imageio/webp-imageio -->
<dependency>
    <groupId>org.sejda.imageio</groupId>
    <artifactId>webp-imageio</artifactId>
    <version>0.1.6</version>
</dependency>

Read WebP image from Java

Now, let’s see how we can read WebP images to a BufferedImage object. You can read the WebP image just like reading any other JPEG or PNGs with ImageIO.read() function. Let’s understand it with a sample code snippet.

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class JavaWebpSupportTest {

  public static void main(String[] args) throws IOException {
    JavaWebpSupportTest webPSupportTest = new JavaWebpSupportTest();
    webPSupportTest.readWebpImage();
  }

  private void readWebpImage() throws IOException {
    BufferedImage image = ImageIO.read(new File("/home/gc/my_image.webp"));
    System.out.printf("\nDimension of the image:%dx%d", image.getWidth(), image.getHeight());
  }
}

This will read the WebP image as BufferedImage and will print the dimension of the image. You can see a sample output below. You can then continue processing the BufferedImage just you do with any other image format.

Java Webp Image Read
Java program successfully read WebP image and displayed its dimension.

Save WebP image from Java

Now that we have learned how to read WebP images, let’s see how we can save a BufferedImage as a WebP image. For this, we will read a JPEG image and then save it as a WebP image. WebP supports for both lossless and lossy compressions. So, let’s explore how to do both cases with Java.

private void convertJpegToWebp() throws Exception {
  //Read JPG image
  BufferedImage image = ImageIO.read(new File("/home/afsal/sample.jpg"));
  // Encode it as webp using default settings and save it as webp file
  ImageIO.write(image, "webp", new File("/home/afsal/sample.webp"));
}

1. Save WebP with lossless encoding

In the above code snippet, we have used the default settings for writing the WebP image file. Now, let’s see how we can customize the encoding parameters to customize the image. First, let’s save the image in lossless format. We will set the compression type as LOSSLESS_COMPRESSION using the WebPWriteParam#setCompressionType option.

import com.luciad.imageio.webp.WebPWriteParam;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;

public class JavaWebpSupportTest {

  public static void main(String[] args) throws Exception {
    JavaWebpSupportTest webPSupportTest = new JavaWebpSupportTest();
    webPSupportTest.convertJpegToWebpLossless();
  }

  private void convertJpegToWebpLossless() throws Exception {
    BufferedImage image = ImageIO.read(new File("/home/gc/sample.jpg"));

    ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();

    WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
    //Notify encoder to consider WebPWriteParams
    writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    //Set lossless compression
    writeParam.setCompressionType(writeParam.getCompressionTypes()[WebPWriteParam.LOSSLESS_COMPRESSION]);

    // Save the image
    writer.setOutput(new FileImageOutputStream(new File("/home/gc/sample.webp")));
    writer.write(null, new IIOImage(image, null, null), writeParam);
  }
}

2. Save WebP with lossy encoding

Now, let’s see how we can configure the compression ratio and use lossy compression to encode an image as WebP. The following code will read a JPEG image and save it as WebP with 80% compression quality (0% has the highest compression and 100% has the highest quality).

private void convertJpegToWebpWithLossyCompression() throws Exception {
  BufferedImage image = ImageIO.read(new File("/home/gc/sample.jpg"));

  ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();

  WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
  //Notify encoder to consider WebPWriteParams
  writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
  //Set lossy compression
  writeParam.setCompressionType(writeParam.getCompressionTypes()[WebPWriteParam.LOSSY_COMPRESSION]);
  //Set 80% quality. Allowed values are between 0 and 1
  writeParam.setCompressionQuality(0.8f);

  // Save the image
  writer.setOutput(new FileImageOutputStream(new File("/home/gc/sample.webp")));
  writer.write(null, new IIOImage(image, null, null), writeParam);
}

Conclusion

In this tutorial, we have learned how to process WebP images using Java. With the help of the webp-imageio library, we can read and write WebP images easily. Since the library works with ImageIO.read() and ImageIO.write(), the processing of images is a breeze. If you are using Linux and wants to enable WebP thumbnail support on your platform, learn how to do that from the following tutorial: How to add WebP thumbnail support on Ubuntu and Linux Mint.

Muhammed Afsal Villan
Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.

20 COMMENTS