拆分多页TIFF图像 - 使用.NET和IronPython

3
我有一张多页扫描的TIFF图像,需要将每一页拆分成单独的文件。
利用.NET框架和C#很容易实现这一点,但由于我使用的计算机上没有安装所有开发工具,所以我选择使用IronPython(通过ipy.exe)快速编写处理逻辑的脚本。
我将Stack Overflow作为“博客”引擎,提供了自己的答案。欢迎评论、建议、替代方案等!
2个回答

3
这里有一种方法可以做到这一点 - 根据需要进行调整。
import clr
clr.AddReference("System.Drawing")

from System.Drawing import Image
from System.Drawing.Imaging import FrameDimension
from System.IO import Path

# sourceFilePath - The full path to the tif image on disk (e.g path = r"C:\files\multipage.tif")
# outputDir - The directory to store the individual files.  Each output file is suffixed with its page number.
def splitImage(sourceFilePath, outputDir):
     img = Image.FromFile(sourceFilePath)

     for i in range(0, img.GetFrameCount(FrameDimension.Page)):

         name = Path.GetFileNameWithoutExtension(sourceFilePath)
         ext = Path.GetExtension(sourceFilePath)
         outputFilePath = Path.Combine(outputDir, name + "_" + str(i+1) + ext)

         frameDimensionId = img.FrameDimensionsList[0]
         frameDimension = FrameDimension(frameDimensionId)

         img.SelectActiveFrame(frameDimension, i)
         img.Save(outputFilePath, ImageFormat.Tiff)

1

这种方法的一个缺点是,图像数据在保存时被解压缩然后重新压缩。如果您使用JPEG压缩来处理TIFF中的图像,则会失去质量,除了时间和内存之外。

有一些使用libtiff直接完成此操作的方法 - 我不知道其他非商业工具可以完成此操作。基本上,您需要在文件中找到与图像数据相关的TIFF目录条目,并将它们直接复制到新的TIFF中,而无需对其进行解码和重新编码。根据您想要做多少,您可能需要修复条目中的偏移量(例如,如果您还要带上元数据)

如果您想要能够在不失去质量的情况下拆分、合并、删除或重新排序TIFF文档(并且速度更快,使用的内存更少),请查看我们公司的产品DotImage,并查看TiffDocument类。 这篇CodeProject文章展示了如何实现


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接