4

We got 50TB of 16bit uncompressed TIF images from a industrial sensor in our server, and we want to compress them all with lossless zip compression using python. Using python because it's easier to use Python to communicate our database.

However after hours of search and documentation reading, I found that there's not even a matured python library that can convert 16bit TIF into zip compressed tif. The latest PIL cannot write compressed tif, OpenCV hardcoded output file into LZW tif not zip(deflate). And there is no sufficient documentation in smc.freeimage, PythonImageMagick so I don't know if they can do it. I also found this tifffile.py, there seems something about compression in its source code, but there is no example code that let me understand how to config compression option for output.

Of course I can use an external executable, but I just don't want to use python as scripting language here.

So that I really appreciate if anyone give me an efficient example here, thanks.

Update:

cgohlke's code works, here I provide another light weight solution. Checkout the patched pythontifflib code from here https://github.com/delmic/pylibtiff.

The original PythonTiffLib from google code doesn't handle RGB information well and it didn't work on my data, this patched version works, however because the code is very old, it implies PythonTiffLib may be not maintained very well.

Use the code like this:

from libtiff import TIFF

tif = TIFF.open('Image.tiff', mode='r')
image = tif.read_image()

tifw = TIFF.open('testpylibtiff.tiff', mode='w')
tifw.write_image(image, compression='deflate', write_rgb=True)
4

1 回答 1

4

PythonMagick 在 Windows 上为我工作:

from PythonMagick import Image, CompressionType
im = Image('tiger-rgb-strip-contig-16.tif')
im.compressType(CompressionType.ZipCompression)
im.write("tiger-rgb-strip-contig-16-zip.tif")

Scikit-image 包含 FreeImage 库的包装器:

import skimage.io._plugins.freeimage_plugin as fi
im = fi.read('tiger-rgb-strip-contig-16.tif')
fi.write(im, 'tiger-rgb-strip-contig-16-zip.tif',
         fi.IO_FLAGS.TIFF_ADOBE_DEFLATE)

或通过tifffile.py,2013.11.03 或更高版本:

from tifffile import imread, imsave
im = imread('tiger-rgb-strip-contig-16.tif')
imsave("tiger-rgb-strip-contig-16-zip.tif", im, compress=6)

这些可能不会保留所有其他 TIFF 标记或属性,但未在问题中指定。

于 2013-09-24T06:37:50.307 回答