[PYTHON IMAGE-SIG] Re: [PIL] How to write compressed TIFF images
Tue, 13 May 1997 17:26:44 +0200
> an hour ago I wanted to show (to my boss) how easy
> it is to do some image manipulation in Python. He
> was quite impressed until I tried to write the
> resulting TIFF image to a file with G4 compression.
> Is it really so? Can't the tiff library be used for
Sure, it only takes someone to do it. And an integrated G4 codec is
in the plans for 0.4 or something, but while you're waiting, here's
some simple code that uses the "tiffcp" utility to write TIFF's of all
flavours. Just import the following module from your program, and you
can then use the "compression" option with the save method as shown in
the test snippet.
# use "tiffcp" to write compressed TIFF files.
# fredrik lundh (may 13, 1997)
import os, tempfile
# install standard driver
import Image, TiffImagePlugin
LZW = "lzw"
ZIP = "zip"
JPEG = "jpeg"
PACKBITS = "packbits"
G3 = "g3"
G4 = "g4"
def _save(im, fp, filename):
# check compression mode
compression = im.encoderinfo["compression"]
# use standard driver
TiffImagePlugin._save(im, fp, filename)
# compress via temporary file
if compression not in (LZW, ZIP, JPEG, PACKBITS, G3, G4):
raise IOError, "unknown compression mode"
file = tempfile.mktemp()
os.system("tiffcp -c %s %s %s" % (compression, file, filename))
if __name__ == "__main__":
im = Image.open("/usr/iv/tip/images/lenna.ppm")
im = im.point(lambda v: v >= 128 and 255, "1")
IMAGE-SIG - SIG on Image Processing with Python
send messages to: email@example.com
administrivia to: firstname.lastname@example.org