Java - resize image without losing quality

前端 未结 7 1546
轮回少年
轮回少年 2020-11-29 15:48

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 unco

相关标签:
7条回答
  • 2020-11-29 16:27

    Below are my own implementation of Progressive Scaling, without using any external library. Hope this help.

    private static BufferedImage progressiveScaling(BufferedImage before, Integer longestSideLength) {
        if (before != null) {
            Integer w = before.getWidth();
            Integer h = before.getHeight();
    
            Double ratio = h > w ? longestSideLength.doubleValue() / h : longestSideLength.doubleValue() / w;
    
            //Multi Step Rescale operation
            //This technique is describen in Chris Campbell’s blog The Perils of Image.getScaledInstance(). As Chris mentions, when downscaling to something less than factor 0.5, you get the best result by doing multiple downscaling with a minimum factor of 0.5 (in other words: each scaling operation should scale to maximum half the size).
            while (ratio < 0.5) {
                BufferedImage tmp = scale(before, 0.5);
                before = tmp;
                w = before.getWidth();
                h = before.getHeight();
                ratio = h > w ? longestSideLength.doubleValue() / h : longestSideLength.doubleValue() / w;
            }
            BufferedImage after = scale(before, ratio);
            return after;
        }
        return null;
    }
    
    private static BufferedImage scale(BufferedImage imageToScale, Double ratio) {
        Integer dWidth = ((Double) (imageToScale.getWidth() * ratio)).intValue();
        Integer dHeight = ((Double) (imageToScale.getHeight() * ratio)).intValue();
        BufferedImage scaledImage = new BufferedImage(dWidth, dHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = scaledImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(imageToScale, 0, 0, dWidth, dHeight, null);
        graphics2D.dispose();
        return scaledImage;
    }
    
    0 讨论(0)
  • 2020-11-29 16:33

    Thumbnailator is a library that was written to create high-quality thumbnails in a simple manner, and doing a batch conversion of existing images is one of its use cases.

    Performing batch resizing

    For example, to adapt your example using Thumbnailator, you should be able to achieve similar results with the following code:

    File folder = new File("/Users/me/Desktop/images/");
    Thumbnails.of(folder.listFiles())
        .size(112, 75)
        .outputFormat("jpg")
        .toFiles(Rename.PREFIX_DOT_THUMBNAIL);
    

    This will go ahead and takes all files in your images directory and proceed to process them one by one, try to resize them to fit in the dimensions of 112 x 75, and it will attempt to preserve the aspect ratio of the original image to prevent "warping" of the image.

    Thumbnailator will go ahead and read all files, regardless of image types (as long as the Java Image IO supports the format, Thumbnailator will process it), perform the resizing operation and output the thumbnails as JPEG files, while tacking on a thumbnail. to the beginning of the file name.

    The following is an illustration of how the file name of the original will be used in the file name of the thumbnail if the above code is executed.

    images/fireworks.jpg     ->  images/thumbnail.fireworks.jpg
    images/illustration.png  ->  images/thumbnail.illustration.png
    images/mountains.jpg     ->  images/thumbnail.mountains.jpg
    

    Generating high-quality thumbnails

    In terms of image quality, as mentioned in Marco13's answer, the technique described by Chris Campbell in his The Perils of Image.getScaledInstance() is implemented in Thumbnailator, resulting in high-quality thumbnails without requiring any complicated processing.

    The following is the thumbnail generated when resizing the fireworks image shown in the original question using Thumbnailator:

    Thumbnail of image in original question

    The above image was created with the following code:

    BufferedImage thumbnail = 
        Thumbnails.of(new URL("http://i.stack.imgur.com/X0aPT.jpg"))
            .height(75)
            .asBufferedImage();
    
    ImageIO.write(thumbnail, "png", new File("24745147.png"));
    

    The code shows that it can also accept URLs as input, and that Thumbnailator is also capable of creating BufferedImages as well.


    Disclaimer: I am the maintainer of the Thumbnailator library.

    0 讨论(0)
  • 2020-11-29 16:33

    After days of research i would prefer javaxt.

    use Thejavaxt.io.Image class has a constructor like:

    public Image(java.awt.image.BufferedImage bufferedImage)
    

    so you can do (another example):

    javaxt.io.Image image = new javaxt.io.Image(bufferedImage);
    image.setWidth(50);
    image.setOutputQuality(1);
    

    Here's the output:

    0 讨论(0)
  • 2020-11-29 16:48

    The result seems to be better (than the result of your program), if you apply Gaussian blur before resizing:

    This is the result I get, with sigma * (scale factor) = 0.3:

    Thumbnail when bluring first(sigma=15.0)

    With ImageJ the code to do this is quite short:

    import ij.IJ;
    import ij.ImagePlus;
    import ij.io.Opener;
    import ij.process.ImageProcessor;
    
    public class Resizer {
    
        public static void main(String[] args) {
            processPicture("X0aPT.jpg", "output.jpg", 0.0198, ImageProcessor.NONE, 0.3);
        }
    
        public static void processPicture(String inputFile, String outputFilePath, double scaleFactor, int interpolationMethod, double sigmaFactor) {
            Opener opener = new Opener();
            ImageProcessor ip = opener.openImage(inputFile).getProcessor();
            ip.blurGaussian(sigmaFactor / scaleFactor);
            ip.setInterpolationMethod(interpolationMethod);
            ImageProcessor outputProcessor = ip.resize((int)(ip.getWidth() * scaleFactor), (int)(ip.getHeight()*scaleFactor));
            IJ.saveAs(new ImagePlus("", outputProcessor), outputFilePath.substring(outputFilePath.lastIndexOf('.')+1), outputFilePath);
        }
    
    }
    

    BTW: You only need ij-1.49d.jar (or equivalent for other version); there's no need to install ImageJ.

    0 讨论(0)
  • 2020-11-29 16:50

    Given your input image, the method from the answer in the first link in the comments (kudos to Chris Campbell) produces one of the following thumbnails:

    enter image description here enter image description here

    (The other one is the thumbnail that you created with MS Paint. It's hard to call one of them "better" than the other...)

    EDIT: Just to point this out as well: The main problem with your original code was that you did not really scale the image in multiple steps. You just used a strange loop to "compute" the target size. The key point is that you actually perform the scaling in multiple steps.

    Just for completeness, the MVCE

    (Edit: I mentioned Chris Campbell and referred to the source via the comments, but to make this more clear here: The following is based on the article The Perils of Image.getScaledInstance() )

    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.Transparency;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.Iterator;
    
    import javax.imageio.IIOImage;
    import javax.imageio.ImageIO;
    import javax.imageio.ImageWriteParam;
    import javax.imageio.ImageWriter;
    import javax.imageio.stream.ImageOutputStream;
    import javax.imageio.stream.MemoryCacheImageOutputStream;
    
    public class ResizeQuality
    {
        public static void main(String[] args) throws IOException
        {
            BufferedImage image = ImageIO.read(new File("X0aPT.jpg"));
            BufferedImage scaled = getScaledInstance(
                image, 51, 75, RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
            writeJPG(scaled, new FileOutputStream("X0aPT_tn.jpg"), 0.85f);
        }
    
        public static BufferedImage getScaledInstance(
            BufferedImage img, int targetWidth,
            int targetHeight, Object hint, 
            boolean higherQuality)
        {
            int type =
                (img.getTransparency() == Transparency.OPAQUE)
                ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
            BufferedImage ret = (BufferedImage) img;
            int w, h;
            if (higherQuality)
            {
                // Use multi-step technique: start with original size, then
                // scale down in multiple passes with drawImage()
                // until the target size is reached
                w = img.getWidth();
                h = img.getHeight();
            }
            else
            {
                // Use one-step technique: scale directly from original
                // size to target size with a single drawImage() call
                w = targetWidth;
                h = targetHeight;
            }
    
            do
            {
                if (higherQuality && w > targetWidth)
                {
                    w /= 2;
                    if (w < targetWidth)
                    {
                        w = targetWidth;
                    }
                }
    
                if (higherQuality && h > targetHeight)
                {
                    h /= 2;
                    if (h < targetHeight)
                    {
                        h = targetHeight;
                    }
                }
    
                BufferedImage tmp = new BufferedImage(w, h, type);
                Graphics2D g2 = tmp.createGraphics();
                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
                g2.drawImage(ret, 0, 0, w, h, null);
                g2.dispose();
    
                ret = tmp;
            } while (w != targetWidth || h != targetHeight);
    
            return ret;
        }
    
        public static void writeJPG(
            BufferedImage bufferedImage,
            OutputStream outputStream,
            float quality) throws IOException
        {
            Iterator<ImageWriter> iterator =
                ImageIO.getImageWritersByFormatName("jpg");
            ImageWriter imageWriter = iterator.next();
            ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
            imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            imageWriteParam.setCompressionQuality(quality);
            ImageOutputStream imageOutputStream =
                new MemoryCacheImageOutputStream(outputStream);
            imageWriter.setOutput(imageOutputStream);
            IIOImage iioimage = new IIOImage(bufferedImage, null, null);
            imageWriter.write(null, iioimage, imageWriteParam);
            imageOutputStream.flush();
        }    
    }
    
    0 讨论(0)
  • 2020-11-29 16:50

    We should not forget a TwelveMonkeys Library

    It contains a really impressive filter collection.

    Usage example:

    BufferedImage input = ...; // Image to resample
    int width, height = ...; // new width/height
    
    BufferedImageOp resampler = new ResampleOp(width, height, ResampleOp.FILTER_LANCZOS);
    BufferedImage output = resampler.filter(input, null);
    
    0 讨论(0)
提交回复
热议问题