I'm using the "Twelve Monkeys" (Github) library to read a .tif/.tiff file in Java 8, add a logo and export the new image as a new .tif/.tiff file:
try {
BufferedImage buffLogo = ImageIO.read(fLogo);
BufferedImage buffInput = ImageIO.read(fImg);
int image_type = buffInput.getType();
//Computing correct position and size of logo here...
BufferedImage buffOutput = new BufferedImage(buffInput.getWidth(), buffInput.getHeight(), image_type);
File out = new File(outputImg);
Graphics2D g = buffOutput.createGraphics();
g.drawImage(buffInput, 0,0,null);
g.drawImage(buffLogo, pos_x,pos_y,size_x,size_y,null);
g.dispose();
System.out.println("Beginning to write to: '"+out.getAbsolutePath()+"'");
long start = System.currentTimeMillis();
ImageIO.write(buffOutput, "tif", out);
long dur = (System.currentTimeMillis() - start) / 1000;
System.out.println("Writing finished successfully ("+dur+"s)!");
} catch (IOException e) {
System.out.println("Exception:\n"+e.getMessage());
}
The input files I've tested this with so far are about 12mb each, TYPE_3BYTE_BGR
and use LZW compression. Exporting as .png only takes a couple of seconds and the files are slightly smaller than the originals. Jpg is even faster and each file is <1mb. If I export as .tif/.tiff, then ImageIO.write
not only takes about 5 minutes to finish writing a single file but the files are also huge, about 25mb with the same type (more with e.g. TYPE_INT_ARGB
but I don't need transparency).
Is that just the way the library works or is there anything specific I have to do, any specific parameters I have to set or other functions to use to make exporting tifs faster and the resulting files a lot smaller (more like the original size, edit: LZW compression would be fine)?