代码之家  ›  专栏  ›  技术社区  ›  Kevin Pullin

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

  •  3
  • Kevin Pullin  · 技术社区  · 15 年前

    我扫描了一个多页TIFF图像,需要将每个页面分割成单独的文件。

    这很容易通过利用.NET框架和C来实现,但是由于我没有在使用的计算机上安装所有的开发工具,所以我选择使用Ironpython(通过ipy.exe)来快速编写处理逻辑脚本。

    使用堆栈溢出作为“博客”引擎,我将为自己的问题提供一个答案。欢迎评论、建议、备选方案等!

    2 回复  |  直到 13 年前
        1
  •  3
  •   Kevin Pullin    15 年前

    有一种方法可以做到这一点-根据需要进行调整。


    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)
    
        2
  •  1
  •   Lou Franco    15 年前

    这样做的一个缺点是图像数据被解压缩,然后在保存时被重新压缩。如果压缩是无损的(只是时间和内存),这不是问题,但是如果对TIFF中的图像使用jpeg压缩,则会降低质量。

    有很多方法可以直接使用libtiff来实现这一点——我不知道还有其他非商业工具可以做到这一点。基本上,您需要在文件中找到与图像数据相关的TIFF目录条目,并将它们直接复制到新的TIFF中,而不需要对它们进行解码和重新编码。根据您想做的程度,您可能需要修正条目中的偏移量(例如,如果您还需要引入元数据)

    如果您有兴趣在不损失质量的情况下拆分、合并、删除TIFF文档中的页面或对其重新排序(而且速度更快、内存使用更少),请查看我公司的产品, DotImage 然后看看 TiffDocument 班级。 This CodeProject article shows how to do it .