Create Multiframe TIFF Image File

From IronPython Cookbook

Create Multiframe TIFF Image File

This example takes a number of input images (PNG files, in this case), and puts them all into a single multiframe TIFF image file. Note that any image file format supported by the Image.FromFile method would work (GIF, BMP, JPEG, PNG, etc).

inputFiles = ['00.png', '01.png', '02.png', '03.png', '04.png']
outputFile = 'output.tiff'

import clr
clr.AddReference('System.Drawing')
from System.Drawing import Image
from System.Drawing.Imaging import (ImageFormat, Encoder, EncoderValue,
     EncoderParameter, EncoderParameters, ImageCodecInfo)

codecInfo = [ic for ic in ImageCodecInfo.GetImageEncoders()
             if ic.FormatDescription == 'TIFF'][0]

encoderParams = EncoderParameters(2)

encoderParams.Param[0] = EncoderParameter(      # multiframe TIFF
    Encoder.SaveFlag, EncoderValue.MultiFrame)
encoderParams.Param[1] = EncoderParameter(      # use LZW compression
    Encoder.Compression, EncoderValue.CompressionLZW)


tiff = Image.FromFile(inputFiles[0])
tiff.Save(outputFile, codecInfo, encoderParams)

# Use the FrameDimensionPage flag on the middle pages:
encoderParams.Param[0] = EncoderParameter(Encoder.SaveFlag,
                                          EncoderValue.FrameDimensionPage)
for inputFile in inputFiles[1:]:
    image = Image.FromFile(inputFile)
    tiff.SaveAdd(image, encoderParams)

# Use the Flush flag after the last page:
encoderParams.Param[0] = EncoderParameter(Encoder.SaveFlag,
                                          EncoderValue.Flush)
tiff.SaveAdd(encoderParams)


Back to Contents.

TOOLBOX
LANGUAGES