将多页 TIFF 映像拆分为单个映像 (Java)

2022-09-03 09:16:19

一直在撕扯我的头发。

如何将多页/多层 TIFF 图像拆分为多个单独的图像?

此处提供演示图像。

(更喜欢纯Java(即非本机)解决方案。如果解决方案依赖于商业库,则无关紧要。


答案 1

您可以使用 Java 高级映像JAI,通过使用 ImageReader 来拆分 mutlipage TIFF:

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);

然后,您可以获取页数:

nbPages = reader.getNumImages(true);

并分别阅读页面:

reader.read(numPage)

答案 2

一个快速但非JAVA的解决方案是。它是 libtiff 库的一部分。tiffsplit

将 tiff 文件拆分到所有图层中的示例命令如下:

tiffsplit image.tif

手册页说明了一切:

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/

推荐