Write an image to file – Java BufferedImage ImageIO
In the following tutorial we demonstrate how to write an image to a file. We can write and convert different images. In this example we read a JPG
file and convert the image to a PNG
, GIF
and BMP
file.
How to Write an Image in Java
This example shows how to write an image in java. We write the file in multiple formats.
JPEG
– Joint Photographic Experts Group.PNG
– Portable Network Graphics.GIF
– Graphic Interchange Format.BMP
– Windows Bitmap Image.TIFF
– Tagged Image File Format.
package com.memorynotfound.image;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class WriteImageExample {
public static void main(String... args) throws IOException {
// read image from folder
URL input = new URL("https://memorynotfound.com/wp-content/uploads/java-duke-to-jpg.jpg");
BufferedImage image = ImageIO.read(input);
ImageIO.write(image, "png", new File("/tmp/duke.png"));
ImageIO.write(image, "gif", new File("/tmp/duke.gif"));
ImageIO.write(image, "bmp", new File("/tmp/duke.bmp"));
ImageIO.write(image, "tiff", new File("/tmp/duke.tiff"));
}
}
Converted Images
To demonstrate the result, we added the following table.
duke.jpg (original) | duke.png | duke.gif | duke.bmp |
---|---|---|---|
Size: 12KB | Size: 46KB | Size: 12KB | Size: 423KB |