如何在python中将图像转换为16位zip(deflate)压缩的TIF?

tom*_*234 4 python tiff image-processing

我们从服务器中的工业传感器获得了 50TB 的 16 位未压缩 TIF 图像,我们希望使用 python 通过无损 zip 压缩来压缩它们。使用 python 因为使用 Python 来与我们的数据库通信更容易。

然而经过几个小时的搜索和阅读文档,我发现甚至没有一个成熟的Python库可以将16位TIF转换为zip压缩的tif。最新的 PIL 无法将压缩的 tif、OpenCV 硬编码输出文件写入 LZW tif 而不是 zip(deflate)。而且 smc.freeimage、PythonImageMagick 中没有足够的文档,所以我不知道他们是否可以做到这一点。我还发现了这个tifffile.py,它的源代码中似乎有一些关于压缩的内容,但没有示例代码让我了解如何配置输出的压缩选项。

当然,我可以使用外部可执行文件,但我只是不想在这里使用 python 作为脚本语言。

因此,如果有人在这里给我一个有效的例子,我真的很感激,谢谢。

更新:

cgohlke的代码有效,这里我提供另一个轻量级解决方案。从此处查看修补后的 pythontifflib 代码https://github.com/delmic/pylibtiff

来自谷歌代码的原始PythonTiffLib不能很好地处理RGB信息,并且它不适用于我的数据,这个修补版本可以工作,但是因为代码非常旧,这意味着PythonTiffLib可能没有得到很好的维护。

使用这样的代码:

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)
Run Code Online (Sandbox Code Playgroud)

cgo*_*lke 5

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")
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

或者通过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)
Run Code Online (Sandbox Code Playgroud)

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