title: How to remove white background of an image - java
body: <p>I would like to remove white background of an image and save it as another image.
  I have wrote a code which extracts the background but it leaves some of the pixel value.
  Checkout original Image : <a href="https://i.stack.imgur.com/aYPvl.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aYPvl.jpg" alt="enter image description here"></a></p>
  
  <p>Checkout cropped image : <a href="https://i.stack.imgur.com/U2Woy.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U2Woy.jpg" alt="enter image description here"></a></p>
  
  <p>It still leaves some amount of white background.</p>
  
  <p>I want to remove that also.</p>
  
  <p>Here is my code :</p>
  
  <pre><code>       int x1=0;
      int y1=0;
      boolean res = false;
      System.out.println("in formatImage");
  </code></pre>
  
  <p>//Widht Removal...</p>
  
  <pre><code>  for (int x = 0; x &lt;= w-1; x++) {
              for (int y = 0; y &lt;= h-1; y++) {
                  if(new Color(bi1.getRGB(x, y)).getRGB()==-1)
                  {res=false;}
                  else if (!new Color(bi1.getRGB(x, y)).equals(Color.WHITE)) {
                      res = true;
                  }
                  if (res) {
                      for (int p = y; p &lt;= h-1; p++) {
                          b21.setRGB(x1,p,new Color(bi1.getRGB(x, p)).getRGB());                        
                      }
                      x1++;
                      res = false;
                      break;
                  }
              }
          }
          b21=new BufferedImage(x1,h,BufferedImage.TYPE_INT_RGB);
          x1=0;
          for (int x = 0; x &lt;= w-1; x++) {
              for (int y = 0; y &lt;= h-1; y++) {
                  if(new Color(bi1.getRGB(x, y)).getRGB()==-1)
                  {res=false;}
                  else if (!new Color(bi1.getRGB(x, y)).equals(Color.WHITE)) {
                      res = true;
                  }
                  if (res) {
                      for (int p = 0; p &lt;= h-1; p++) {
                          b21.setRGB(x1,p,new Color(bi1.getRGB(x, p)).getRGB());
                      }
                      x1++;
                      res = false;
                      break;
                  }
              }
          }
  </code></pre>
  
  <p>//Height Removal        </p>
  
  <pre><code>for (int y = 0; y &lt;= h-1; y++) {
      System.out.println("Y = "+y);
      for (int x = 0; x &lt;= x1-1; x++) {
          System.out.println("("+x+","+y+") : "+b21.getRGB(x, y));
          if (!new Color(b21.getRGB(x, y)).equals(Color.WHITE)) {
              res = true;
          }
          if (res) {
              for (int p = 0; p &lt;= x1-1; p++) {
                 b31.setRGB(p,y1,new Color(b21.getRGB(p, y)).getRGB());
  
              }
              y1++;
              res = false;
              break;
          }
      }
  }
  b31=new BufferedImage(x1,y1,BufferedImage.TYPE_INT_RGB);
  int ty=y1;
  y1=0;
  for (int y = 0; y &lt;= h-1; y++) {
      for (int x = 0; x &lt;= x1-1; x++) {
          if (!new Color(b21.getRGB(x, y)).equals(Color.WHITE)) {
              res = true;
          }
          if (res) {
              for (int p = 0; p &lt;= x1-1; p++) {
                 b31.setRGB(p,y1,new Color(b21.getRGB(p, y)).getRGB());
              }
              y1++;
              res = false;
              break;
          }
      }
  }
  </code></pre>
  
  <p>b31 has final image.</p>