Link
Skip to main content

Java PNG Writer

Java’s built-in ImageIO has limited support for writing PNG files and provides little control over encoding quality and format-specific options. JDeli is a 100% Java PNG writer that gives you full encoding control with no dependencies.

Getting started

Add JDeli to your project via Maven or Gradle, or see using JDeli with Java modules.

What is supported

The JDeli PNG writer supports the following features:

Feature Support
Compression - NONE yes
Compression - 8bit Quantised yes
Compression - Zlib yes
Alpha yes
RGB yes
Palette yes
Grayscale yes
Filtering yes
ICC No
Metadata EXIF
Output to file Yes
Output to OutputStream Yes
Output to byte[] Yes
Pure Java implementation Yes

Quick start or to replace in existing code using ImageIO:

The simplest way to write a PNG image is:

JDeli.write(bufferedImage, "png", file);

Or to a byte array:

byte[] pngData = JDeli.write(bufferedImage, "png");

Or to an OutputStream:

JDeli.write(bufferedImage, "png", os);

Simple usage

For better type safety, use OutputFormat:

JDeli.write(myBufferedImage, OutputFormat.PNG, outputStreamOrFile);

OutputFormat allows setting of any supported Image Format

Benefits:

  • Compile-time safety
  • Cleaner code
  • Recommended for new applications

Advanced usage: controlling the output

Use pngEncoderOptions when you need control over encoding behaviour.

final PngEncoderOptions options = new PngEncoderOptions();

//set any options in options instance - examples below
options.setCompressionFormat(PngCompressionFormat.QUANTISED8BIT);
options.setCompressionFormat(PngCompressionFormat.NONE);

//write out
JDeli.write(myBufferedImage, options, outputStreamOrFile);

PngEncoderOptions allows setting of specific options.

Various image processing operations can be conducted on the image, detailed documentation can be found here.

Performance comparisons:

These figures were generated using jmh (as documented on our blog) with a standard set of images (also documented). They should be easy to replicate if you wish to validate, the code is on GitHub.

The higher the number, the better.
Benchmark results using JMH:
Mode: Throughput Count: 25 Units: ops/s

Benchmark Score Error
Apache 5.100 ± 0.018
ImageIO 4.845 ± 0.034
ImageIO_fast 5.529 ± 0.115
ImageIO_max_comp 3.512 ± 0.008
JDeli 5.383 ± 0.033
JDeli_quantised 1.192 ± 0.001
JDeli_fast 13.061 ± 0.026
JDeli_compressed 5.402 ± 0.027
JDeli_uncompressed 12.985 ± 0.088

Number of files: 166

Benchmark Size unreadable output AVG SSIM score AVG Similarity
Apache 16.129KB 0 0.9437771308658404 Similar (noticeable but minor differences)
ICafe 10.731KB 0 0.9397287642484 Similar (noticeable but minor differences)
ImageIO_fast 16.926KB 0 1.0 Identical or virtually identical
ImageIO_max_comp 16.174KB 0 1.0 Identical or virtually identical
JDeli_compressed 16.172KB 0 1.0 Identical or virtually identical
JDeli_fast 16.924KB 0 1.0 Identical or virtually identical
JDeli_quantised 3.170KB 0 0.9588066625523578 Very similar (high quality)
JDeli_uncompressed 16.924KB 0 1.0 Identical or virtually identical

Tested on 2020 13inch M1 MacBook Pro using JDK 18.0.1.1


Frequently asked questions

Does JDeli produce better PNG encoding than Java ImageIO?

Yes. While ImageIO can write PNG, JDeli gives you full control over encoding options such as compression and quality, and produces more consistent output across platforms.

Does writing or encoding PNG in Java with JDeli require additional libraries?

No. JDeli is a 100% Java library with no dependencies. It runs on any platform where the JVM runs with no additional installation.

What output types does JDeli support for writing PNG?

JDeli can write and encode PNG to a File, OutputStream, or return the encoded data as a byte array.

See also


Why JDeli?

  • Support image formats such as AVIF, HEIC and JPEG XL that are not supported in Java.
  • Process images up to 3x faster than ImageIO and alternative Java image libraries.
  • Prevent JVM crashes caused by native code in other image libraries such as ImageIO.
  • Handle JPEG, PNG, TIFF image file formats fully in Java.
  • Keep your Image files secure as JDeli makes no calls to any external system or third party library.

Learn more about JDeli

Start Your Free Trial