Convert PNG to WEBP in Java
JDeli is a pure Java library that converts PNG to WEBP without native dependencies. It accepts File, InputStream, and byte[] input, and supports batch conversion from the command line.
PNG (Portable Network Graphics) is a lossless format widely used for web graphics and images requiring transparency. Converting PNG to other formats may reduce file size or meet specific platform requirements.
Why use JDeli instead of Java ImageIO
Java’s built-in ImageIO cannot write WEBP. JDeli provides a pure Java encoder with no native dependencies required.
To run the code examples you will need to download the JDeli jar:
Convert PNG to WEBP in one line of code
The JDeli.convert() methods allow you to save PNG as WEBP in just ONE line of code!
Using File
JDeli.convert(File inFile, File outFile);
JDeli.convert(File inFile, EncoderOptions encoderOptions, File outfile);
JDeli.convert(File inFile, File outfile, ImageProcessingOps imageProcessingOps);
Using InputStream and OutputStream
JDeli.convert(InputStream inputStream, OutputStream outputStream, "webp");
JDeli.convert(InputStream inputStream, EncoderOptions encoderOptions, OutputStream outputStream);
JDeli.convert(InputStream inputStream, OutputStream outputStream, ImageProcessingOps imageProcessingOps);
Using byte[]
byte[] outputData = JDeli.convert(byte[] inputData, "webp");
byte[] outputData = JDeli.convert(byte[] inputData, EncoderOptions encoderOptions);
byte[] outputData = JDeli.convert(byte[] inputData, "webp", ImageProcessingOps imageProcessingOps);
Batch convert PNG to WEBP from command line
Change image formats from command line or bash, bat, and powershell scripts. This method can also be used to invoke JDeli from any programming language that allows you to create a child process.
java -jar jdeli.jar --convert webp "inputFileOrDir" "outputDir"
How to convert from PNG into WEBP in Java as separate steps
Sometimes there is a requirement to convert between formats as separate steps. JDeli also allows the image to be read in as a BufferedImage and then written out later.
- Read PNG image into Java
BufferedImage bufferedImage = JDeli.read(new File("pngImageFile.png")); - Process image if needed (scale, sharpen, lighten, watermark, etc)
bufferedImage = operations.apply(BufferedImage bufferedImage); // Optional - Write out BufferedImage as WEBP image file
JDeli.write(bufferedImage, "webp", new File("webpImageFile.webp"));
Frequently asked questions
Does Java support WEBP natively?
No. ImageIO cannot write WEBP. JDeli provides a pure Java encoder with no native dependencies required.
Can I convert PNG to WEBP without installing native libraries?
Yes. JDeli is a pure Java library. It runs on any platform where the JVM runs with no additional installation.
What input types does JDeli accept for PNG conversion?
JDeli accepts File, InputStream, and byte[] as input. Output can be written to a File, OutputStream, or returned as byte[].
Can I process the image during conversion?
Yes. All JDeli.convert() variants accept an ImageProcessingOps parameter, so you can scale, sharpen, watermark, and apply other operations in the same step. The two-step read/write approach also supports this if you need more control.
Related conversions
See the JDeli conversion overview for a full list of supported format conversions.