# Creating grayscale image of original picture

Converting an image to grayscale is really simple. You can create grayscale image by setting same value for Red, Green and Blue. It means RGB encoding supports 256 level of grays from (0,0,0), (1,1,1) throught (150,150,150) and finally (255,255,255). Tricky part to create grayscale image is to find replicated value or value level to form grayscale. For this we can find intensity of color by averaging color components. We all know there are three color components involved to form a color. (Red+Green+Blue)/3. Find the method below that produce fine results.

```// Create grayscale of original image
public void grayscale() {
// Get all pixel
Pixel[] pixelArray = this.getPixels();

// Iterate through all pixel
for (int index = 0; index < pixelArray.length; index++) {

// computing intensity by averaging color components
int intensity = (int) (pixelArray[index].getRed() + pixelArray[index].getGreen()
+ pixelArray[index].getBlue()) / 3;
// create a new color
Color color = new Color(intensity, intensity, intensity);

// set new color
pixelArray[index].setColor(color);
}
}```

There is another technique to compute grayscale that considers luminance perceived by human eye. Human normally considers blue darker then red even if same amount of light reflected off. Here we weight blue lower and red more when computing average.

```// Create grayscale of original image
public void grayscaleWithLuminance() {
// Get all pixel
Pixel[] pixelArray = this.getPixels();

// Iterate through all pixel
for (int index = 0; index < pixelArray.length; index++) {

// computing intensity by averaging color components
int luminance = (int) ((pixelArray[index].getRed()*0.299) + (pixelArray[index].getGreen()*0.587)
+ (pixelArray[index].getBlue()*0.114));
// create a new color
Color color = new Color(luminance, luminance, luminance);

// set new color
pixelArray[index].setColor(color);
}
}```