How to combine multiple PNGs into one big PNG file?

后端 未结 9 2027
天涯浪人
天涯浪人 2020-11-28 08:26

I have approx. 6000 PNG files (256*256 pixels) and want to combine them into a big PNG holding all of them programmatically.

What\'s the best/fastest way to do that?

9条回答
  •  遥遥无期
    2020-11-28 09:05

    Create a large image which you will write to. Work out its dimensions based on how many rows and columns you want.

        BufferedImage result = new BufferedImage(
                                   width, height, //work these out
                                   BufferedImage.TYPE_INT_RGB);
        Graphics g = result.getGraphics();
    

    Now loop through your images and draw them:

        for(String image : images){
            BufferedImage bi = ImageIO.read(new File(image));
            g.drawImage(bi, x, y, null);
            x += 256;
            if(x > result.getWidth()){
                x = 0;
                y += bi.getHeight();
            }
        }
    

    Finally write it out to file:

        ImageIO.write(result,"png",new File("result.png"));
    

提交回复
热议问题