How to Convert PNG to WEBP with Java REST API

This step by step tutorial shows you how to convert PNG to WEBP with Java REST API. PNG (Portable Network Graphics) is a type of image file format that supports lossless data compression. PNG files are often used for web graphics and images with transparent backgrounds. They are also commonly used for saving high-quality graphics for printing.

However, WEBP is an image file format developed by Google, which is used for lossless and lossy compression of digital images. It is based on the WebM video format, which was developed for use in HTML5 video elements. WEBP supports both lossy and lossless compression, and is designed to provide faster loading times for webpages containing images. If you want PNG to WEBP Conversion in Java Low Code API then this can be carried out with the help of the following sample.

Prerequisite

Steps to Convert PNG to WEBP in Java REST API

  1. Set Client ID and Client Secret for the API
  2. Create an object of ImagingAPI class with client credentials
  3. Specify input and output files
  4. Read input PNG file and upload to cloud storage
  5. Create an object of ConvertImageRequest with input and output file formats
  6. Call convertImage method to Convert PNG to WEBP using REST API
  7. Save the output WEBP file on local disk

Code for PNG to WEBP Conversion in Java Low Code API

String ClientID = Client.getID(); //replace Client.getID() with your own client ID here
String ClientSecret = Client.getSecret(); //replace Client.getSecret() with your own client secret here
String APIBaseUrl="https://api.aspose.cloud";
String Local_Path = "C:/Temp/";
ImagingApi imagingApiSdk = new ImagingApi(ClientSecret, ClientID, APIBaseUrl);
// Input & output file names
String inputFileName = "PNGtoWEBP.png";
String outputFileName = "PNGtoWEBP.webp";
try {
// Upload local image to Cloud Storage
File inputFile = new File(Local_Path + inputFileName);
FileInputStream inputFileStream = new FileInputStream(inputFile);
byte[] inputImageData = IOUtils.toByteArray(inputFileStream);
UploadFileRequest uploadFileRequest = new UploadFileRequest(inputFileName, inputImageData, null);
FilesUploadResult filesUploadResult = imagingApiSdk.uploadFile(uploadFileRequest);
String outputFormat = "webp";
String remoteFolder = null; // Input file is saved at the root of the storage
String remoteStorage = null; // Cloud Storage name
ConvertImageRequest convertImageRequest = new ConvertImageRequest(inputFileName, outputFormat, remoteFolder, remoteStorage);
byte[] convertedImageData = imagingApiSdk.convertImage(convertImageRequest);
// Save exported image to local storage
FileOutputStream fileOutputStream = new FileOutputStream(Local_Path + outputFileName);
fileOutputStream.write(convertedImageData);
fileOutputStream.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}

The code snippet given in this post empowers you to convert PNG to WEBP with Java REST API. You simply have to upload PNG file with the help of the Aspose.Imaging REST API SDK and download output WEBP file to save it locally.

This PNG to WEBP Conversion feature can be exercised with any no code or low code apps on any operating system.

Please check out a related feature at the following link: How to Convert JPG to WEBP with Java REST API

 English