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

发布于 2024-08-14 07:25:43 字数 205 浏览 7 评论 0原文

我有一个扫描的多页 TIFF 图像,需要将每个页面拆分为单独的文件。

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

使用 Stack Overflow 作为“博客”引擎,我将回答我自己的问题。欢迎提出意见、建议、替代方案等!

I had a scanned multipage TIFF image and needed to split each page out into individual files.

This is easy to do in by leveraging the .NET framework and C#, but since I did not have all the development tools installed on the machine I was using, I instead opted to use IronPython (via ipy.exe) to quickly script the processing logic.

Using Stack Overflow as a 'blog' engine, I'll provide an answer to my own question. Comments, suggestions, alternatives, etc. are welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹎☆浅夏丿初晴 2024-08-21 07:25:43

这是执行此操作的一种方法 - 根据需要进行调整。


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)

Here is one way to do this - tweak as needed.


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)
携余温的黄昏 2024-08-21 07:25:43

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

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

如果您对能够拆分、合并、删除页面或重新排序 TIFF 文档感兴趣在不损失质量的情况下(而且速度更快,使用更少的内存),请看一下我公司的产品,DotImage ,然后查看 TiffDocument 类。 这篇 CodeProject 文章介绍了如何执行此操作

One downside to doing it this way is that the image data was decompressed and then re-compressed when it was saved. This is not a problem if your compression is lossless (just time and memory), but if you are using JPEG compression for the images inside the TIFF, you will lose quality.

There are ways to do this using libtiff directly -- I don't know of any other non-commercial tools that can do it. Basically, you need to find the TIFF directory entries in the file that relate to the image data and copy them directly into a new TIFF without decoding them and reencoding. Depending on how much you want to do, you may need to fix offsets in the entries (e.g. if you are also bringing over the meta-data)

If you are interested in being able to split, merge, remove pages from or reorder TIFF documents without losing quality (and also faster and using less memory), take a look at my company's product, DotImage, and look at the TiffDocument class. This CodeProject article shows how to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文