Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a multipage TIFF image into individual images (Java)

Tags:

java

tiff

Been tearing my hair on this one.

How do I split a multipage / multilayer TIFF image into several individual images?

Demo image available here.

(Would prefer a pure Java (i.e. non-native) solution. Doesn't matter if the solution relies on commercial libraries.)

like image 298
aioobe Avatar asked Jul 21 '13 07:07

aioobe


People also ask

How do I split multiple images in a TIFF?

❓ How can I split TIFF document? First, you need to add a file for split: drag & drop your TIFF file or click inside the white area for choose a file. Then click the 'Split' button. When split TIFF document is completed, you can download your result files.

How do I convert a multipage TIFF file to JPEG?

Hit CTRL + P, or navigate to the printing dialog by clicking File > Print… From the list of available printers, choose Universal Document Converter, then select the Properties option. When the Properties window is displayed, choose Load Settings… From the list of available settings, select PDF to JPEG.

Can TIFF store multiple images?

TIFF files can store an unlimited number of separate images. Each image has an IFD, or Image File Directory, that records where that image's data and metadata is stored in the file.

Can I split a multipage TIFF file into single TIFF files?

Unless you want to save the individual pages to different image format, there is no point in decoding the image. Given the special structure of the TIFF image, you can split a multipage TIFF into single TIFF images without decoding.

Can ImageMagick split TIF files into individual images?

- ImageMagick Split multipage TIF,TIFF images into individual images and RETAIN original filename after conversion. Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs.

How to append multiple images to an existing TIFF file?

How to append multiple images to existing single page or multi-page TIFF files. You can load as many images as you want by clicking the Load Images button. All these images will be shown in panels. Now you can click the AddtoExistingFile button. An open file dialog will open and you can specify a file name by selecting any TIFF file from ...

How do I get the number of pages in a tif file?

You need to instantiate an Image object using the Image class static method FromFile (). Then we need to determine how many pages exist in this single .TIF file by calling the Image class GetFrameCount () method and set the parameter by passing the FrameDimension.Page enumerator.


2 Answers

You can use the Java Advanced Imaging library, JAI, to split a mutlipage TIFF, by using an ImageReader:

ImageInputStream is = ImageIO.createImageInputStream(new File(pathToImage));
if (is == null || is.length() == 0){
  // handle error
}
Iterator<ImageReader> iterator = ImageIO.getImageReaders(is);
if (iterator == null || !iterator.hasNext()) {
  throw new IOException("Image file format not supported by ImageIO: " + pathToImage);
}
// We are just looking for the first reader compatible:
ImageReader reader = (ImageReader) iterator.next();
iterator = null;
reader.setInput(is);

Then you can get the number of pages:

nbPages = reader.getNumImages(true);

and read pages separatly:

reader.read(numPage)
like image 155
Thire Avatar answered Sep 22 '22 08:09

Thire


A fast but non JAVA solution is tiffsplit. It is part of the libtiff library.

An example command to split a tiff file in all it's layers would be:

tiffsplit image.tif

The manpage says it all:

NAME
       tiffsplit - split a multi-image TIFF into single-image TIFF files

SYNOPSIS
       tiffsplit src.tif [ prefix ]

DESCRIPTION
       tiffsplit  takes  a multi-directory (page) TIFF file and creates one or more single-directory (page) TIFF files
       from it.  The output files are given names created by concatenating a prefix, a lexically ordered suffix in the
       range  [aaa-zzz],  the  suffix  .tif (e.g.  xaaa.tif, xaab.tif, xzzz.tif).  If a prefix is not specified on the
       command line, the default prefix of x is used.

OPTIONS
       None.

BUGS
       Only a select set of ‘‘known tags’’ is copied when splitting.

SEE ALSO
       tiffcp(1), tiffinfo(1), libtiff(3TIFF)

       Libtiff library home page: http://www.remotesensing.org/libtiff/
like image 38
CousinCocaine Avatar answered Sep 23 '22 08:09

CousinCocaine