Image Processing in Java - Read and Write

Last Updated : 9 Jan, 2026

Java provides a built-in class called BufferedImage to represent images in memory. A BufferedImage allows direct access to pixel data and color information, making it suitable for image manipulation and processing.

Java supports reading and writing multiple image formats such as PNG, JPG, BMP, and GIF using the ImageIO API. Support for additional formats (like HEIC) can be added using third-party libraries such as Apache Imaging or JDeli.

Why BufferedImage?

  • Abstracts away image format complexities
  • Stores image data in RAM
  • Allows pixel-level manipulation
  • Works seamlessly with ImageIO

Once an image is loaded, all operations are performed on the BufferedImage object, regardless of the original image format.

Classes Required to Perform the Read and Write Operations:

  • java.io.File: To read and write an image file, we must import the File class. This class represents file and directory path names in general.
  • java.io.IOException: To handle errors, we use the IOException class.
  • java.awt.image.BufferedImage: To hold the image, we create the BufferedImage object; we use BufferedImage class. This object is used to store an image in RAM.
  • javax.imageio.ImageIO: To perform the image read-write operation, we will import the ImageIO class. This class has static methods to read and write an image.

Example: Read and Write an Image in Java

This reads an image from disk into a BufferedImage object and then writes it back to disk in PNG format. It demonstrates basic image I/O operations using Java’s built-in ImageIO API.

Java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class MyImage {
    public static void main(String[] args) throws IOException {

        BufferedImage image = null;
        try {
            File inputFile = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png");

            image = ImageIO.read(inputFile);
            System.out.println("Image read successfully.");
        }
        catch (IOException e) {
            System.out.println("Error while reading image: " + e.getMessage());
        }
        try {
            File outputFile = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/gfg.png");

            ImageIO.write(image, "png", outputFile);
            System.out.println("Image written successfully.");
        }
        catch (IOException e) {
            System.out.println("Error while writing image: " + e.getMessage());
        }
    }
}

Output

Note: This code will not run on online IDE as it needs an image on disk.

Explanation:

  • The image is read from disk using ImageIO.read(File), which returns a BufferedImage.
  • The image is stored entirely in memory, allowing further processing if required.
  • The same image is then written back to disk using ImageIO.write().
  • Java automatically determines the appropriate image writer based on the file format.
Comment