Skip to main content

Merging Tiff's

13 replies [Last post]
grevsmuehl
Offline
Joined: 2006-05-16

I have attempted many different approaches to appending one tif image to to the bottom af another image. With every approach I get the same result. The combined output image only contains the first image I write out and there is no sign of the second image. I have tried the write, writeInsert, and writeToSequence methods of the ImageWriter and have received the same result each time. Ihave also tried using the JAI.create and the ImageIO.read commands to create the create the incoming images but still get the same results. Here is a code snippet of what I am trying to do:

ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("tiff");
if (iter.hasNext()) {
writer = (ImageWriter)iter.next();
}

InputStream ins0 = new FileInputStream("c:/Andy/JAI/image1.tif");
BufferedImage img0 = ImageIO.read(ins0);
InputStream ins1 = new FileInputStream("c:/Andy/JAI/image2.tif");
BufferedImage img1 = ImageIO.read(ins1);

ImageOutputStream ios = ImageIO.createImageOutputStream(new File("c:/Andy/JAI/image12.tif"));
writer.setOutput(ios);

(ImageWriteParam.MODE_EXPLICIT);
// iwp.setCompressionQuality(compressionQuality);

writer.write(new IIOImage(img0, null, null));
writer.writeInsert(1, new IIOImage(img1, null, null), null);

ios.flush();
writer.dispose();
ios.close();

Any help on why I only see the first image would be geratly appreciated.

Thanks,
Andy

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
yuvaraju460
Offline
Joined: 2008-12-04

plazee give me the complete source code f this issue plz plz send me argently

vivekbakshi
Offline
Joined: 2009-03-14

Hi Brian,

I also have a similar requirement of merging multiple tiffs into one. I have used the code from this post, but running in errors while trying to merge. Every time I execute my code I get an exception:

java.lang.IllegalStateException: No TIFF reader found
at com.tiff.TiffMerging.concateTiff(TiffMerging.java:27)
at com.tiff.TiffMerging.main(TiffMerging.java:88)

Please let me know what I am missing. It is really Urgent.

Your Immediate help on this would be highly appreciated.

Thanks,
Vivek

jigar1986
Offline
Joined: 2008-02-05

Can u tell me pls how save all tiff images in single file...

its really urgent...

Thanx in advance

dpmesa
Offline
Joined: 2006-06-22

Hi,

Could you please let me know how you have it working.

thanks!

Brian Burkhalter

These query leaves a lot to interpretation ...

On Thu, 22 Jun 2006, jai-interest@javadesktop.org wrote:

> Hi,
>
> Could you please let me know how you have it working.
>
> thanks!
> [Message sent by forum member 'dpmesa' (dpmesa)]
>
> http://forums.java.net/jive/thread.jspa?messageID=126254
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>
>

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

subrahmanyam_adapa
Offline
Joined: 2008-09-08

Iterator writers = ImageIO.getImageWritersByFormatName("tiff");ImageWriter writer = (ImageWriter) writers.next();ImageOutputStream ios = null;ios = ImageIO.createImageOutputStream(new FileOutputStream("d:\\tiffmanipulation_code_poc\\mergedFile.tiff"));
writer.setOutput(ios);InputStream ins = new FileInputStream ("D:/tiffmanipulation_code_poc/single_0.tiff");
BufferedImage firstImg = ImageIO.read(ins);
writer.write(new IIOImage(firstImg, null, null));
for(int i=1;i<5;i++){InputStream ins1 = new FileInputStream("D:/tiffmanipulation_code_poc/single_"+i+".tiff");
BufferedImage img1 = ImageIO.read(ins1);
writer.writeInsert(-1, new IIOImage(img1, null, null), null); }

akstaj_004
Offline
Joined: 2011-10-09

Hello subrahmanyam_adapa,

I've installed JAI 1.1 in my system & when I run a example of merging two TIFF images it says Appropriate TIFF writer not found.

Can you help me with this error?

Here's the code I am trying to implement:

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.IIOImage;
import javax.imageio.ImageWriteParam;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;

/**
*
*
* This class is used to convert the multiple images in to single multi-pages tiff image file.
*
*/
public class MultiPageTiffGenerator{

private static String compressionType="JPEG";

public static boolean generateMultiPageTiff(String dirName,String outputFileName) throws Exception{
boolean generated=false;
ImageOutputStream ios=null;
ImageWriter writer=null;
try {
// verify the directory for the image files
File dir = new File(dirName);
if(null!=dir && !dir.isDirectory()){
System.out.println("No directory exists with the given name");
throw new FileNotFoundException("No directory exists with the given name"+dirName);
}
// verify the images files exist

File[] files = dir.listFiles();
if(null == files || files.length == 0){
System.out.println("No image files to process");
throw new FileNotFoundException("No image files to process");

}else{
// Create the output file on the disk
File file = new File(dirName+outputFileName+".tif");
ios = ImageIO.createImageOutputStream(file);

// Get the appropriate Tiff Image Writer
Iterator <ImageWriter> writers=ImageIO.getImageWritersByFormatName("tiff");
if(null== writers || ! writers.hasNext()){
System.out.println("Appropriate Tiff writer not found");
throw new Exception("Appropriate Tiff writer not found");
}

writer = ImageIO.getImageWritersByFormatName("tiff").next();
writer.setOutput(ios);
// Set the compression parameters for Tiff image
ImageWriteParam param = writer.getDefaultWriteParam();
//param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
//param.setCompressionType(compressionType);
//param.setCompressionQuality(0.9F);

// Loop through all image files and write them to output tiff image
for (int i = 0; i < files.length; i++) {
InputStream fis=null;
int dotIndex= files[i].getName().lastIndexOf('.');
dotIndex=dotIndex>0?dotIndex:files[i].getName().length();
String fileName = files[i].getName().substring(0,dotIndex);
System.out.println("file name "+fileName);
if(!fileName.equalsIgnoreCase(outputFileName)){
try{

fis = new BufferedInputStream(new FileInputStream(files[i]));
BufferedImage image = ImageIO.read(fis);
IIOImage img = new IIOImage(image, null, null);
if (i == 0) {
writer.write(null, img, param);
}
else {
writer.writeInsert(-1, img, param);
}
image.flush();
}finally{
if(null!=fis){
fis.close();
}
}
}

}
ios.flush();
generated=true;

}

}catch(FileNotFoundException ex){
generated=false;
}catch(IOException ex){
generated=false;
}catch(Exception ex){
generated=false;
}finally{
if(null!=ios)
ios.close();
if(null!=writer)
writer.dispose();
}
return generated;
}

public static void main(String args[]){
try{
boolean res = generateMultiPageTiff("C:\\test","03");
System.out.println("Result "+res);
}catch(Exception e){
System.out.println(e);
}
}
}

I read your example but its quite unclear for me as I am new to this.

Can you please provide a complete source code.

Please Help.

subrahmanyam_adapa
Offline
Joined: 2008-09-08

InputStream ins1 = new FileInputStream ("D:/tiffmanipulation_code_poc/single_"+i+".tiff");
BufferedImage img1 = ImageIO.read(ins1);
writer.writeInsert(-1, new IIOImage(img1, null, null), null); }

(Please add the following lines after the for loop of above reply to Mert single images as one MergedFile)

grevsmuehl
Offline
Joined: 2006-05-16

I finally got the merging of the tiff's to work, is there a similar way to merge jpeg's?

Brian Burkhalter

On Wed, 17 May 2006, jai-interest@javadesktop.org wrote:

> I finally got the merging of the tiff's to work,

So what was the problem?

> is there a similar way to merge jpeg's?

I don't believe that the ITU T.81 compressed data format specification
accomodates multiple frames. Therefore if you have multiple JPEG images in a
single stream they will just be the concatenation of individual complete JPEG
single-image streams. You should be able to write such a concatenation using
either the Java SE of the JAI Image I/O Tools JPEG writer. In either case you
would simply write the successive images as if they were isolated while making
sure not to change the position of the stream between writes.

Brian

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

rockys
Offline
Joined: 2008-10-30

Please can u give me the source code for merging two tiff files into one tiff file.
I am using jdk1.5 with JAI 1.2

I have used the following code :
try{
File f = new File(inputPath);
String fileList[] = f.list();
ImageWriter writer = null;
Iterator writers = ImageIO.getImageWritersByFormatName("tiff");
while(writers.hasNext()){
writer = (ImageWriter)writers.next();
}
ImageOutputStream ios = null;
ios = ImageIO.createImageOutputStream(new FileOutputStream(outputPath+"\\"+"00000001.tiff"));
writer.setOutput(ios);
InputStream ins = new FileInputStream (inputPath+"\\"+"000000010.tiff");
BufferedImage firstImg = ImageIO.read(ins);
writer.write(new IIOImage(firstImg, null, null));
for(int i=1;i<4;i++){
String fileName = fileList[i].substring(0,fileList[i].lastIndexOf(".")-1);
String inputFilePath = inputPath+"\\"+fileName+i+".tiff";
InputStream ins1 = new FileInputStream (inputFilePath);
BufferedImage img1 = ImageIO.read(ins1);
writer.writeInsert(i,new IIOImage(img1, null, null),null);
}
}catch(Exception e){
e.printStackTrace();
}

amolc8183
Offline
Joined: 2010-12-06

Hi,
I used the above mentioned code to merge tiff file in our application but found out that the resulting merged tiff file is having huge size. For instance when 5 files each of size 300kb are merged into one the size of merged file created is approx. 18MB.
Do you think it could be issue with compression format and we need to use appropriate compression format to reduce the size of the file. Will it help?
Thanks,
Amol

Andrey Kuznetsov

>I have attempted many different approaches to appending one tif image to to
>the bottom af another image.

So now I don't understand anymore what do you want to do:
1) Merge some TIFF files = create multipage TIFF
or
2) make one big image from some smaller images and save it as TIFF (1 page
only)

Andrey

--
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net