Open In App

Java Program to Create Grayscale Image

The RGB model where red, green and blue lights are added in various different intensities and permutations and combinations of them producing broad spectrum array of colors whereas grayscale refers to the white and black colors only where the weakest intensity gives birth to black color and strongest as pure white. 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.



Approach: 

In order to reach to the goal the approach is likely treating the image as a multidimensional array where every element is equivalent to a pixel. Now we will simply iterate over every pixel. Further, iterating over each pixel and calculate the RGB gray scale colors and adjust it to grayscale. 



Procedure:

We will use some in-built methods of BufferedImage class. Classes required to perform the operation:

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

Implementation:

Example




// Java Program to Create Grayscale Image
// implementation to Blur RGB image
 
// Importing required libraries
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws IOException, InterruptedException
    {
        // Declaring an array to hold color spectrum
        Color color[];
 
        // Reading the image in the form of file
        // from the directory
        File fin = new File("D:/test/Image.jpeg");
 
        // Convert file into image form by
        // creating object of BufferedImage class
        BufferedImage input = ImageIO.read(fin);
 
        // Now creating output Image
        BufferedImage output = new BufferedImage(
            input.getWidth(), input.getHeight(),
            BufferedImage.TYPE_INT_RGB);
 
        // Setting attributes to image
        int i = 0;
        int max = 400, rad = 10;
        int a1 = 0, r1 = 0, g1 = 0, b1 = 0;
        color = new Color[max];
 
        // core section responsible for blurring of image
        int x = 1, y = 1, x1, y1, ex = 5, d = 0;
 
        // Running loop for each pixel and blurring it
        // Nested for loops
        for (x = rad; x < input.getHeight() - rad; x++) {
            for (y = rad; y < input.getWidth() - rad; y++) {
                for (x1 = x - rad; x1 < x + rad; x1++) {
                    for (y1 = y - rad; y1 < y + rad; y1++) {
                        color[i++] = new Color(
                            input.getRGB(y1, x1));
                    }
                }
 
                // Smoothing colors of image to
                // get grayscaled corresponding image
 
                i = 0;
 
                for (d = 0; d < max; d++) {
                    a1 = a1 + color[d].getAlpha();
                }
 
                a1 = a1 / (max);
                for (d = 0; d < max; d++) {
                    r1 = r1 + color[d].getRed();
                }
 
                r1 = r1 / (max);
                for (d = 0; d < max; d++) {
                    g1 = g1 + color[d].getGreen();
                }
 
                g1 = g1 / (max);
                for (d = 0; d < max; d++) {
                    b1 = b1 + color[d].getBlue();
                }
 
                b1 = b1 / (max);
                int sum1 = (a1 << 24) + (r1 << 16)
                           + (g1 << 8) + b1;
                output.setRGB(y, x, (int)(sum1));
            }
        }
 
        // Finally writing the blurred image on the disc
        // specifying type and location
        // to be written on machine
        ImageIO.write(
            output, "jpeg",
            new File("D:/test/BlurredImage.jpeg"));
 
        // Display message for successful execution of
        // program
        System.out.println("Image blurred successfully !");
    }
}

Output:

Image blurred successfully !

Also demonstrating visual representation where after executing the program, the console will pop up of successful build and run of the program leading to grayscaling of the image as shown below


Article Tags :