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?
As others have pointed out, using Java is not necessarily the best bet here.
If you're going to use Java, your best bet--assuming you're sufficiently short on memory so that you can't read the entire dataset into memory multiple times and then write it out again--is to implement RenderedImage
with a class that will read your PNGs off the disk upon demand. If you just create your own new BufferedImage and then try to write it out, the PNG writer will create an extra copy of the data. If you create your own RenderedImage, you can pass it to ImageIO.write(myImageSet,"png",myFileName)
. You can copy SampleModel
and ColorModel
information from your first PNG--hopefully they're all the same.
If you pretend that the entire image is multiple tiles (one tile per source image), then ImageIO.write
will create a WritableRaster
that is the size of the entire image data set, and will call your implementation of RenderedImage.copyData
to fill it with data. If you have enough memory, this is an easy way to go (because you get a huge target set of data and can just dump all your image data into it--using the setRect(dx,dy,Raster)
method--and then don't have to worry about it again). I haven't tested to see whether this saves memory, but it seems to me that it should.
Alternatively, if you pretend that the whole image is a single tile, ImageIO.write
will then ask, using getTile(0,0)
, for the raster that corresponds to that entire image. So you have to create your own Raster, which in turn makes you create your own DataBuffer. When I tried this approach, the minimum memory usage that successfully wrote a 15360x25600 RGB PNG was -Xmx1700M
(in Scala, incidentally), which is just barely over 4 bytes per pixel of written image, so there's very little overhead above one full image in memory.
The PNG data format itself is not one that requires the entire image in memory--it would work okay in chunks--but, sadly, the default implementation of the PNG writer assumes it will have the entire pixel array in memory.
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"));
Combing Images
private static void combineALLImages(String screenNames, int screens) throws IOException, InterruptedException {
System.out.println("screenNames --> D:\\screenshots\\screen screens --> 0,1,2 to 10/..");
int rows = screens + 1;
int cols = 1;
int chunks = rows * cols ;
File[] imgFiles = new File[chunks];
String files = "";
for (int i = 0; i < chunks; i++) {
files = screenNames + i + ".jpg";
imgFiles[i] = new File(files);
System.out.println(screenNames + i + ".jpg"+"\t Screens : "+screens);
}
BufferedImage sample = ImageIO.read(imgFiles[0]);
//Initializing the final image
BufferedImage finalImg = new BufferedImage(sample.getWidth() * cols, sample.getHeight() * rows, sample.getType());
int index = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
BufferedImage temp = ImageIO.read(imgFiles[index]);
finalImg.createGraphics().drawImage(temp, sample.getWidth() * j, sample.getHeight() * i, null);
System.out.println(screenNames + index + ".jpg");
index++;
}
}
File final_Image = new File("D:\\Screenshots\\FinalImage.jpg");
ImageIO.write(finalImg, "jpeg", final_Image);
}
You might be best off bouncing things off another (lossless) image format. PPM is dead easy to use (and to put tiles in programmatically; it's just a big array on disk, so you'll only have to store one row of tiles at most), but it's very wasteful of space (12 bytes per pixel!).
Then use a standard converter (e.g. ppm2png
) that takes the intermediary format and turns it into the giant PNG.
I do not see how it would be possible "without processing and re-encoding". If you insist on using Java then I just suggest you to use JAI (project page here). With that you would create one big BufferedImage, load smaller images and draw them on the bigger one.
Or just use ImageMagick montage
:
montage *.png output.png
For more information about montage
, see usage.
The PNG format has no support for tiling, so there is no way you can escape at least decompressing and recompressing the data stream. If the palettes of all images are identical (or all absent), this is the only thing you really need to do. (I'm also assuming the images aren't interlaced.)
You could do this in a streaming way, only having open one "row" of PNGs at a time, reading appropriately-sized chunks from their data stream and writing them to the output stream. This way you would not need to keep entire images in memory. The most efficient way would be to program this on top of libpng yourself. You may need to keep slightly more than one scanline of pixels in memory because of the pixel prediction.
But just using the command-line utilities of ImageMagick, netpbm or similar will save you a large amount of development time for what may be little gain.