有没有人有在 Python 中导入每 channel 16 位、3 channel TIFF 图像的方法?
我还没有找到一种在处理 TIFF 格式时可以保留每个 channel 16 位深度的方法。我希望一些有帮助的人能找到解决办法。
以下是我迄今为止尝试过但没有成功的方法和结果:
import numpy as np
import PIL.Image as Image
import libtiff
import cv2
im = Image.open('a.tif')
# IOError: cannot identify image file
tif = libtiff.TIFF.open('a.tif')
im = tif.read_image()
# im only contains one of the three channels. im.dtype is uint16 as desired.
im = []
for i in tif.iter_images():
# still only returns one channel
im = np.array(cv2.imread('a.tif'))
# im.dtype is uint8 and not uint16 as desired.
# specifying dtype as uint16 does not correct this
到目前为止,我发现的唯一解决方案是使用 ImageMagick 将图像转换为 PNG。然后沼泽标准 matplotlib.pyplot.imread
读取 PNG 文件没有任何问题。
我遇到的另一个问题是将任何 numpy 数组保存为 16 位 PNG 文件,到目前为止这也不是很简单。
最佳答案
它的功能有限,尤其是在将非 RGB 图像回写到磁盘时,但是 Christoph Gohlke's tifffile
module读取 3 channel 16 位 TIFF 没有问题,我刚刚测试过:
>>> import tifffile as tiff
>>> a = tiff.imread('Untitled-1.tif')
>>> a.shape
(100L, 100L, 3L)
>>> a.dtype
dtype('uint16')
而且 Photoshop 阅读时不会提示我所做的事情:
>>> tiff.imsave('new.tiff', a)
关于Python:读写TIFF 16位,三 channel ,彩色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18446804/