Splitting TIFF Multipage Images - with .NET and IronPython

I had a scanned multi-page TIFF image and you had to split each page into separate files.

This is easy to do using the .NET platform and C #, but since I did not have all the development tools installed on the computer that I used, I decided instead to use IronPython (via ipy.exe) for quick script processing logic.

Using Qaru as a blog engine, I will give an answer to my question. Comments, suggestions, alternatives, etc. Welcome!

+3
source share
2 answers

Here is one way to do this - adjust if necessary.


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)
+3
source

One drawback of this is that the image data was unpacked and then recompressed when it was saved. This is not a problem if lossless compression (just time and memory), but if you use JPEG compression for images inside TIFF, you will lose quality.

libtiff - , . , TIFF , TIFF . , , (, )

, , TIFF ( ), DotImage TiffDocument. CodeProject , .

+1

Source: https://habr.com/ru/post/1725228/


All Articles