我没有找到关于PIL方法的任何TIFF文档。我尝试着解决这个问题,但只得到了“坏模式”或“不支持的文件类型”错误。
在这里我需要使用什么?
首先,我从此页面下载了一个名为a_image.tif
的测试TIFF图像。然后我用PIL打开它,代码如下:
>>> from PIL import Image
>>> im = Image.open('a_image.tif')
>>> im.show()
这显示了一张彩虹图片。要转换成numpy数组,只需:
>>> import numpy
>>> imarray = numpy.array(im)
我们可以看到图像的尺寸与数组的形状相匹配:
>>> imarray.shape
(44, 330)
>>> im.size
(330, 44)
而且该数组包含 uint8
值:
>>> imarray
array([[ 0, 1, 2, ..., 244, 245, 246],
[ 0, 1, 2, ..., 244, 245, 246],
[ 0, 1, 2, ..., 244, 245, 246],
...,
[ 0, 1, 2, ..., 244, 245, 246],
[ 0, 1, 2, ..., 244, 245, 246],
[ 0, 1, 2, ..., 244, 245, 246]], dtype=uint8)
当你完成对数组的修改后,你可以像这样将其转换回 PIL 图像:
>>> Image.fromarray(imarray)
<Image.Image image mode=L size=330x44 at 0x2786518>
我使用matplotlib读取TIFF文件:
import matplotlib.pyplot as plt
I = plt.imread(tiff_file)
并且I
将会是ndarray
类型。
根据文档,当处理TIFF文件时,实际上是PIL在后台工作,因为matplotlib本身只能原生地读取PNG格式的文件,但对于我来说这已经很好用了。
还有一个plt.imsave
函数可用于保存文件。
matplotlib
改变了策略:ValueError: Only know how to handle extensions: ['png']; with Pillow installed matplotlib can handle more images
。意思是,似乎 matplotlib
改变了处理图片的方式,如果安装了 Pillow
库,它可以处理更多类型的图片。 - strpeterPyLibTiff 对我来说比 PIL 更好用,因为截至 2023 年 4 月,仍不支持每个颜色超过 8 位的彩色图像。
from libtiff import TIFF
tif = TIFF.open('filename.tif') # open tiff file in read mode
# read an image in the current TIFF directory as a numpy array
image = tif.read_image()
# read all images in a TIFF file:
for image in tif.iter_images():
pass
tif = TIFF.open('filename.tif', mode='w')
tif.write_image(image)
您可以使用以下方式安装PyLibTiff:
pip3 install numpy pylibtiff
tifffile
库,但我还没有尝试过。您也可以使用GDAL来实现此操作。我知道它是一个地理空间工具包,但没有任何要求您必须拥有制图产品。
在这里提供预编译的Windows GDAL二进制文件链接(假设您正在使用Windows):链接
要访问数组:
from osgeo import gdal
dataset = gdal.Open("path/to/dataset.tiff", gdal.GA_ReadOnly)
for x in range(1, dataset.RasterCount + 1):
band = dataset.GetRasterBand(x)
array = band.ReadAsArray()
xrange
不是拼写错误,xrange
是 Python 2 版本的 range
。我接受了这个编辑,因为 Python 3 仍在积极改进,而 Python 2 则不是。 - Taku对于图像堆栈,我发现使用scikit-image
进行读取,使用matplotlib
进行显示或保存更容易。我已经使用以下代码处理了16位TIFF图像堆栈。
from skimage import io
import matplotlib.pyplot as plt
# read the image stack
img = io.imread('a_image.tif')
# show the image
plt.imshow(img,cmap='gray')
plt.axis('off')
# save the image
plt.savefig('output.tif', transparent=True, dpi=300, bbox_inches="tight", pad_inches=0.0)
import pytiff
with pytiff.Tiff("filename.tif") as handle:
part = handle[100:200, 200:400]
# multipage tif
with pytiff.Tiff("multipage.tif") as handle:
for page in handle:
part = page[100:200, 200:400]
它是一个相对较小的模块,并且可能没有其他模块那么多的功能,但它支持平铺的TIFF和BigTIFF格式,因此您可以读取大图像的部分内容。
有一个很棒的包叫做tifffile
,可以让处理.tif或.tiff文件变得非常简单。
使用pip安装包:
pip install tifffile
from tifffile import tifffile
image = tifffile.imread('path/to/your/image')
# type(image) = numpy.ndarray
如果您想将一个numpy数组保存成.tif/.tiff文件:
tifffile.imwrite('my_image.tif', my_numpy_data, photometric='rgb')
或者
tifffile.imsave('my_image.tif', my_numpy_data)
pip install tifffile
was not sufficient for me. You might need also pip install imagecodecs
- mathfuximport cv2
image = cv2.imread(tiff_file.tif)
cv2.imshow('tif image',image)
geoTiff
保存tiff编码,可以使用rasterio
包。import rasterio
out = np.random.randint(low=10, high=20, size=(360, 720)).astype('float64')
new_dataset = rasterio.open('test.tiff', 'w', driver='GTiff',
height=out.shape[0], width=out.shape[1],
count=1, dtype=str(out.dtype),
)
new_dataset.write(out, 1)
new_dataset.close()
如果你想了解更多关于numpy转GEOTiff的内容,可以点击这个链接:https://gis.stackexchange.com/questions/279953/numpy-array-to-gtiff-using-rasterio-without-source-raster
读取tiff文件的另一种方法是使用tensorflow api
import tensorflow_io as tfio
image = tf.io.read_file(image_path)
tf_image = tfio.experimental.image.decode_tiff(image)
print(tf_image.shape)
输出:
(512, 512, 4)
可以在这里找到tensorflow文档。
为了使此模块正常工作,必须安装一个名为tensorflow-io的Python包。
尽管我无法找到一种方法来查看输出张量(转换为nd.array后),因为输出图像有4个通道。我尝试使用cv2.cvtcolor()
将其转换为cv2.COLOR_BGRA2BGR
标志,参考这篇文章,但仍然无法查看图像。
DecompressionBombError: Image size (900815608 pixels) exceeds limit of 178956970 pixels, could be decompression bomb DOS attack.
- devforfu