title: Can't get gray scale of an image in java
body: <p>I have a problem with getting gray scale of a .jpg file. I am trying to create a new .jpg file as gray scaled but I am just copying the image nothing more. Here is my code:</p>
  
  <pre><code>package training01;
  
  import java.awt.*;
  import java.awt.image.BufferedImage;
  
  import java.io.*;
  
  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 = ImageIO.read(input);
              width = image.getWidth();
              height = image.getHeight();
              for(int i = width;i &lt; width;i++) {
                  for(int j = height;j &lt; height;j++) {
                      Color c =  new Color(image.getRGB(i, j));
                      int red = c.getRed();
                      int green = c.getGreen();
                      int blue = c.getBlue();
                      int val = (red+green+blue)/3;
                      Color temp = new Color(val,val,val);
                      image.setRGB(i, j, temp.getRGB());
                  }
              }
              File output = new File("digital_image_processing1.jpg");
              ImageIO.write(image, "jpg", output);
          }catch(Exception e) {
              System.out.println(e);
          }
      }
      public static void main(String[] args) {
          GrayScale gs = new GrayScale();
      }
  }
  </code></pre>