title: Java - resize image without losing quality
body: <p>I have 10,000 photos that need to be resized so I have a Java program to do that. Unfortunately, the quality of the image is poorly lost and I don't have access to the uncompressed images.</p>
  
  <pre><code>import java.awt.Graphics;
  import java.awt.AlphaComposite;
  import java.awt.Graphics2D;
  import java.awt.Image;
  import java.awt.RenderingHints;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  
  
  import javax.imageio.ImageIO;
  /**
   * This class will resize all the images in a given folder
   * @author 
   *
   */
  public class JavaImageResizer {
  
      public static void main(String[] args) throws IOException {
  
          File folder = new File("/Users/me/Desktop/images/");
          File[] listOfFiles = folder.listFiles();
          System.out.println("Total No of Files:"+listOfFiles.length);
          BufferedImage img = null;
          BufferedImage tempPNG = null;
          BufferedImage tempJPG = null;
          File newFilePNG = null;
          File newFileJPG = null;
          for (int i = 0; i &lt; listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                  System.out.println("File " + listOfFiles[i].getName());
                  img = ImageIO.read(new File("/Users/me/Desktop/images/"+listOfFiles[i].getName()));
                  tempJPG = resizeImage(img, img.getWidth(), img.getHeight());
                  newFileJPG = new File("/Users/me/Desktop/images/"+listOfFiles[i].getName()+"_New");
                  ImageIO.write(tempJPG, "jpg", newFileJPG);
                }
          }
          System.out.println("DONE");
      }
  
      /**
       * This function resize the image file and returns the BufferedImage object that can be saved to file system.
       */
          public static BufferedImage resizeImage(final Image image, int width, int height) {
      int targetw = 0;
      int targeth = 75;
  
      if (width &gt; height)targetw = 112;
      else targetw = 50;
  
      do {
          if (width &gt; targetw) {
              width /= 2;
              if (width &lt; targetw) width = targetw;
          }
  
          if (height &gt; targeth) {
              height /= 2;
              if (height &lt; targeth) height = targeth;
          }
      } while (width != targetw || height != targeth);
  
      final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      final Graphics2D graphics2D = bufferedImage.createGraphics();
      graphics2D.setComposite(AlphaComposite.Src);
      graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
      graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
      graphics2D.drawImage(image, 0, 0, width, height, null);
      graphics2D.dispose();
  
      return bufferedImage;
  }
  </code></pre>
  
  <p>An image I am working with is this:
  <img src="https://i.stack.imgur.com/X0aPT.jpg" alt="Firwork - original - large"></p>
  
  <p>This is the manual resizing I've done in Microsoft Paint:</p>
  
  <p><img src="https://i.stack.imgur.com/bbPis.jpg" alt="resize - using Paint - small"></p>
  
  <p>and this is the output from my program [bilinear]:</p>
  
  <p><img src="https://i.stack.imgur.com/Z7tD3.jpg" alt="resize - using java program - small"></p>
  
  <p><strong>UPDATE:</strong> No significant difference using <code>BICUBIC</code></p>
  
  <p>and this is the output from my program [bicubic]:</p>
  
  <p><img src="https://i.stack.imgur.com/oMU52.jpg" alt="enter image description here"></p>
  
  <p>is there anyway to increase the quality of the program output so I don't have to manually resize all photos?</p>
  
  <p>Thank you in advance!</p>