# Image Processing in Java – Colored Image to Grayscale Image Conversion

• Difficulty Level : Medium
• Last Updated : 14 Nov, 2021

Prerequisites:

In this article, we will be converting a colored image to a grayscale image.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

RGB Color Model – The RGB color model is an additive mixing model in which red, green, and blue light are added together in various ways to reproduce a broad array of colors.

Grayscale Images – Grayscale images, a kind of black-and-white or gray monochrome, are composed exclusively of shades of gray. The contrast ranges from black at the weakest intensity to white at the strongest.

Generally, a grayscale image uses an 8-bit representation for each pixel. By using 8-bits, we can represent values from 0 to 255. So a grayscale image in 8-bit representation will be a matrix, and the values can be anything from 0 to 255. 0 indicates black pixels, and 255 indicates white pixels, and in between different shades from black to white will come.

Note: In a grayscale image, the Alpha component of the image will be the same as the original image, but the RGB will be changed i.e, all three RGB components will have the same value for each pixel.

#### Algorithm:

1. Get the RGB value of the pixel.
2. Find the average of RGB, i.e., Avg = (R+G+B)/3
3. Replace the R, G, and B values of the pixel with the average (Avg) calculated in step 2.
4. Repeat Step 1 to Step 3 for each pixel of the image.

## Java

 `// Java program to demonstrate``// colored to grayscale conversion`` ` `import` `java.awt.image.BufferedImage;``import` `java.io.File;``import` `java.io.IOException;``import` `javax.imageio.ImageIO;`` ` `public` `class` `Grayscale {``    ``public` `static` `void` `main(String args[])``        ``throws` `IOException``    ``{``        ``BufferedImage img = ``null``;``        ``File f = ``null``;`` ` `        ``// read image``        ``try` `{``            ``f = ``new` `File(``                ``"C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png"``);``            ``img = ImageIO.read(f);``        ``}``        ``catch` `(IOException e) {``            ``System.out.println(e);``        ``}`` ` `        ``// get image's width and height``        ``int` `width = img.getWidth();``        ``int` `height = img.getHeight();`` ` `        ``// convert to grayscale``        ``for` `(``int` `y = ``0``; y < height; y++) {``            ``for` `(``int` `x = ``0``; x < width; x++) {``               ` `                ``// Here (x,y)denotes the coordinate of image``                ``// for modifying the pixel value.``                ``int` `p = img.getRGB(x, y);`` ` `                ``int` `a = (p >> ``24``) & ``0xff``;``                ``int` `r = (p >> ``16``) & ``0xff``;``                ``int` `g = (p >> ``8``) & ``0xff``;``                ``int` `b = p & ``0xff``;`` ` `                ``// calculate average``                ``int` `avg = (r + g + b) / ``3``;`` ` `                ``// replace RGB value with avg``                ``p = (a << ``24``) | (avg << ``16``) | (avg << ``8``)``                    ``| avg;`` ` `                ``img.setRGB(x, y, p);``            ``}``        ``}`` ` `        ``// write image``        ``try` `{``            ``f = ``new` `File(``                ``"C:/Users/hp/Desktop/Image Processing in Java/GFG.png"``);``            ``ImageIO.write(img, ``"png"``, f);``        ``}``        ``catch` `(IOException e) {``            ``System.out.println(e);``        ``}``    ``}``}`

#### Output –

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