Tag: WebP

  • Java WebP Image Support – Read and Save WebP Images

    Java WebP Image Support – Read and Save WebP Images

    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.

  • How to add WebP thumbnail support on Ubuntu and Linux Mint

    How to add WebP thumbnail support on Ubuntu and Linux Mint

    As of writing this article, on 2022 June, there is no default support for WebP images in the Ubuntu/Linux Mint file explorers. As a result, the image file thumbnails are not rendered and we have to open the image file to see what’s in it. In this tutorial, we will see how to add support for the WebP files so that the image thumbnails are generated just like any other supported image format.

    What is WebP Image Format?

    WebP is an image format developed specifically for the web by Google. The primary goal of the WebP format is to replace other conventional formats like JPEG, PNG, etc. and provide better compression and quality/file-size ratio. When compared to JPEG, WebP file size is 25%-34% for the comparable quality JPEG image.

    Adding WebP thumbnail support

    To add support for WebP images, we need to install the package named “webp-pixbuf-loader”. You can install them by executing the following commands.

    #Add the PPA
    sudo add-apt-repository ppa:krifa75/eog-ordissimo
    
    #Refresh apt
    sudo apt update
    
    #Install the package
    sudo apt install webp-pixbuf-loader
    

    After the installation is done, just refresh the file-manager and you will see the thumbnails generated for the WebP images.

    Webp Thumbnails shown on Nemo file manager

    Image Viewer with WebP Support

    Web browsers like Google Chrome can act as WebP image-viewer. However, if you are looking for image-viewer program itself with support for WebP, you can try gThumb.

    How to install gThumb on Ubuntu/Mint

    sudo apt install gthumb
    

    This will install the gThum image viewer and you can use it to open WebP images.

    Conclusion

    In this tutorial, we have seen how to add native support for WebP images so that the file manager can show thumbnails for them. Also, gThumb image viewer has good support for the new WebP image standard. You might also be interested in reading the following articles.