Java DIP - GrayScale Conversion


In order to convert a color image to Grayscale image, you need to read pixels or data of the image using File and ImageIO objects, and store the image in BufferedImage object. Its syntax is given below −

File input = new File("digital_image_processing.jpg");
BufferedImage image =;	

Further, get the pixel value using method getRGB() and perform GrayScale() method on it. The method getRGB() takes row and column index as parameter.

Color c = new Color(image.getRGB(j, i));
int red = (c.getRed() * 0.299);
int green =(c.getGreen() * 0.587);
int blue = (c.getBlue() *0.114);

Apart from these three methods, there are other methods available in the Color class as described briefly −

Sr.No. Method & Description


It creates a new Color that is a brighter version of this Color.



It creates a new Color that is a darker version of this Color.



It returns the alpha component in the range 0-255.


getHSBColor(float h, float s, float b)

It creates a Color object based on the specified values for the HSB color model.


HSBtoRGB(float hue, float saturation, float brightness)

It converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model.



It returns a string representation of this Color.

The last step is to add all these three values and set it again to the corresponding pixel value. Its syntax is given below −

int sum = red+green+blue;
Color newColor = new Color(sum,sum,sum);


The following example demonstrates the use of Java BufferedImage class that converts an image to Grayscale −

import java.awt.*;
import java.awt.image.BufferedImage;


import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class GrayScale {

   BufferedImage  image;
   int width;
   int height;
   public GrayScale() {
      try {
         File input = new File("digital_image_processing.jpg");
         image =;
         width = image.getWidth();
         height = image.getHeight();
         for(int i=0; i<height; i++) {
            for(int j=0; j<width; j++) {
               Color c = new Color(image.getRGB(j, i));
               int red = (int)(c.getRed() * 0.299);
               int green = (int)(c.getGreen() * 0.587);
               int blue = (int)(c.getBlue() *0.114);
               Color newColor = new Color(red+green+blue,
         File ouptut = new File("grayscale.jpg");
         ImageIO.write(image, "jpg", ouptut);
      } catch (Exception e) {}
   static public void main(String args[]) throws Exception {
      GrayScale obj = new GrayScale();


When you execute the given example, it converts the image digital_image_processing.jpg to its equivalent Grayscale image and write it on hard disk with the name grayscale.jpg.

Original Image

Grayscale Conversion Tutorial

Grayscale Image

Java Image Processing Tutorial