简体   繁体   中英

Merged two images --> 4 times the size! How do I reduce the file size?

I merge two images using the code below. One base image without transparency, one overlay image with transparency. The file-size of the images one there own is 20kb and 5kb, respectively. Once I merged the two images, the resulting file-size is > 100kb, thus at least 4 times the combined size of 25kb. I expected a file-size less than 25kb.

public static void mergeTwoImages(BufferedImage base, BufferedImage overlay, String destPath, String imageName) {

    // create the new image, canvas size is the max. of both image sizes
    int w = Math.max(base.getWidth(), overlay.getWidth());
    int h = Math.max(base.getHeight(), overlay.getHeight());
    BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    // paint both images, preserving the alpha channels
    Graphics2D g2 = combined.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.drawImage(base, 0, 0, null );
    g2.drawImage(overlay, 0, 0, null);
    g2.dispose();

    // Save as new image
    saveImage(combined, destPath + "/" + imageName + "_merged.png");
}

My application has to be with very good performance, thus can anyone explain me why this effect happens and how I can reduce the resulting file size?

Thanks a lot!

EDIT: Thanks a lot for your answers. The saveImage code is:

public static void saveImage(BufferedImage src, String file) {
    try {
        File outputfile = new File(file);
        ImageIO.write(src, "png", outputfile);
    } catch (IOException e) {
        e.printStackTrace();
    }       
}

Because PNG is a lossless format, there are only two major factors that are likely to impact the file size:

  1. How many pixels are in the file, and
  2. How well the format can be compressed.

Since it sounds like you're doing an overlay, I'm guessing #1 is not changing. Compare the pixel dimensions of the input and output files to double-check this.

Most likely you're seeing issues because your merged image is more complicated, so the PNG filtering algorithms have a harder time compressing the files. There's not much you can do about this, short of changing the images or switching to a lossy file format.

To explain just a bit further, let's say you have one all-white image and one all-red image. Both are 100x100 pixels. These images would be really easy to compress because you'd just need to encode: repeat red 10000 times. Now, say you merge these images in a way that every other pixel comes from a different image. Now it's checkered. If you have a good encoding mechanism set up, you might still be able to encode this well by saying: repeat [red,white] 10000 times. But you'll notice even with this ideal encoding algorithm, I've increased the size of my encoded message by quite a bit. And if you don't have an encoding format that's perfectly ideal for this sort of thing, it all goes downhill from there.

In general, the more varied and random-seeming the pixels of your image are, compared to one another, the larger the resulting file will be.

Save the image as JPEG with a higher compression ratio/lower quality. For further details see:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM