Java GIF Writer
Java’s built-in ImageIO has limited support for writing GIF files and provides little control over encoding quality and format-specific options. JDeli is a 100% Java GIF 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 GIF writer supports the following features:
| Feature | Support |
|---|---|
| Compression - LZW | Yes |
| Single color transparency | Yes |
| Multiple frames | Yes |
| Palette optimization | Yes |
| 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 GIF image is:
JDeli.write(bufferedImage, "gif", file);
Or to a byte array:
byte[] gifData = JDeli.write(bufferedImage, "gif");
Or to an OutputStream:
JDeli.write(bufferedImage, "gif", os);
Simple usage
For better type safety, use OutputFormat:
JDeli.write(myBufferedImage, OutputFormat.GIF, 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 gifEncoderOptions when you need control over encoding behaviour.
final GifEncoderOptions options = new GifEncoderOptions();
//write out
JDeli.write(myBufferedImage, options, outputStreamOrFile);
GifEncoderOptions 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:
|Benchmark | Score | Error |
|:——–:|:—–:|:——-:|
|Apache | 1.705 | ± 0.251 |
|ImageIO | 4.713 | ± 0.025 |
|JDeli | 0.538 | ± 0.003 |
Number of files: 166
| Benchmark | AVG Size | unreadable output | AVG SSIM score | AVG Similarity |
|---|---|---|---|---|
| Apache | 3.554KB | 0 | 0.8492693486300651 | Moderately similar (visible differences) |
| ICafe | 2.691KB | 0 | 0.9113843372930903 | Similar (noticeable but minor differences) |
| ImageIO | 2.494KB | 4 | 0.8834616780939093 | Similar (noticeable but minor differences) |
| JDeli | 4.349KB | 0 | 0.9334679980045937 | Similar (noticeable but minor differences) |
Frequently asked questions
Does JDeli produce better GIF encoding than Java ImageIO?
Yes. While ImageIO can write GIF, JDeli gives you full control over encoding options such as compression and quality, and produces more consistent output across platforms.
Does writing or encoding GIF 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 GIF?
JDeli can write and encode GIF to a File, OutputStream, or return the encoded data as a byte array.