GrayScale Conversion Tutorial


In order to convert an colour image to GrayScale image, first thing you need to do is to read pixels or data of the image Using File and ImageIO object and store it an BufferedImage object. Its syntax is given below:

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

After that , get the value of pixel frm getRGB method to grab a pixel value 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 availaible in the Color class which are listed below.

Sr.NoMethod & Description
Creates a new Color that is a brighter version of this Color
Creates a new Color that is a darker version of this Color
Returns the alpha component in the range 0-255
4getHSBColor(float h, float s, float b)
Creates a Color object based on the specified values for the HSB color model
5HSBtoRGB(float hue, float saturation, float brightness)
Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model
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 to convert 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 run the above example, it would convert an image name digital_image_processing.jpg to its equivalent GrayScale image and write it on hard disk with name grayscale.jpg.

Original Image

Grayscale Conversion Tutorial

GrayScale Image

Java Image Processing Tutorial