title: How to get 0..255 colors from a picture in Java?
body: <p>I have tried to grayscale a already black-white-gray picture and it become black. </p>
  
  <p>When I try to grayscale a picture with Java, I do like this:</p>
  
  <pre><code>    // This turns the image data to grayscale and return the data
      private static RealMatrix imageData(File picture) {
          try {
              BufferedImage image = ImageIO.read(picture);
              int width = image.getWidth();
              int height = image.getHeight();
              RealMatrix data = MatrixUtils.createRealMatrix(height * width, 1);
              // Convert to grayscale
              int countRows = 0;
              for (int y = 0; y &lt; height; y++) {
                  for (int x = 0; x &lt; width; x++) {
                      // Turn image to grayscale
                      int p = image.getRGB(x, y);
                      int r = (p &gt;&gt; 16) &amp; 0xff;
                      int g = (p &gt;&gt; 8) &amp; 0xff;
                      int b = p &amp; 0xff;
  
                      // calculate average and save
                      int avg = (r + g + b) / 3;
                      data.setEntry(countRows, 0, avg);
                      countRows++;
                  }
              }
              return data;
          } catch (Exception e) {
              e.printStackTrace();
              return null;
          }
  
      }
  </code></pre>
  
  <p>The problem what I see is that <code>p</code> is an 32-bit value and I only want 8-bit value. Even if the picture is already grayscaled, the <code>p</code> value is already a 32-bit value. That cause trouble for me.</p>
  
  <p>So if I grayscale a gray picture, it will become black. Or at least darker. 
  And I want 0..255 values of <code>p</code>, which is a 32-bit integer value.</p>
  
  <p>Do you have any suggestions how to read pictures as they where 8-bit?
  It's for image classification.</p>
  
  <p><strong>Summarize:</strong></p>
  
  <p>I need help to get each pixels from a picture in 0..255 format.
  One way is to gray scale it, but how can I verify if the picture is already gray scaled? </p>
  
  <p><strong>Update:</strong></p>
  
  <p>I have tried to read a picture as it was 8-bit values. It works. Then I try to save the picture with the same values. The picture becomes very dark.</p>
  
  <p>I have a matlab example I want to show.
  First I read my picture:</p>
  
  <pre><code>image = imread("subject01.normal");
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/QWsdg.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QWsdg.gif" alt="enter image description here"></a></p>
  
  <p>And then I save the picture.</p>
  
  <pre><code>imwrite(uint8(image), "theSameImage.gif")
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/suaub.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/suaub.gif" alt="enter image description here"></a></p>
  
  <p>If I try with a minimal Java code snipped for reading an image.</p>
  
  <pre><code>private static void imageData(File picture) {
          try {
              BufferedImage image = ImageIO.read(picture);
              int width = image.getWidth();
              int height = image.getHeight();
              DataBuffer buffer = image.getRaster().getDataBuffer();
              for (int y = 0; y &lt; height; y++) {
                  for (int x = 0; x &lt; width; x++) {
                      int p = buffer.getElem(x + y * width);
                      image.setRGB(x, y, p);
                  }
              }
              File output = new File(picture.getName());
              ImageIO.write(image, "gif", output);
              return data;
          } catch (Exception e) {
              e.printStackTrace();
              return null;
          }
      }
  </code></pre>
  
  <p>I will get this picture:</p>
  
  <p><a href="https://i.stack.imgur.com/HNdpY.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HNdpY.gif" alt="enter image description here"></a></p>
  
  <p>So even if there is a marked answer in this question, it's still not going to help you. </p>