Jak*_*kob 67 python tiff numpy python-imaging-library
我需要一个python例程,可以打开并将TIFF图像导入到numpy数组中,这样我就可以分析和修改包含的数据,然后再将它们保存为TIFF.(它们基本上是灰度的光强度图,表示每个像素的相应值)
我试图找到一些东西,但没有关于TIFF的PIL方法的文档.我试图搞清楚,但只有错误的模式/文件类型不支持错误.
我需要在这里使用什么?
jte*_*ace 86
首先,我从网上下载的测试TIFF图像,此页面称为a_image.tif
.然后我像这样用PIL打开:
>>> from PIL import Image
>>> im = Image.open('a_image.tif')
>>> im.show()
Run Code Online (Sandbox Code Playgroud)
这显示了彩虹图像.要转换为numpy数组,它很简单:
>>> import numpy
>>> imarray = numpy.array(im)
Run Code Online (Sandbox Code Playgroud)
我们可以看到图像的大小和数组的形状匹配:
>>> imarray.shape
(44, 330)
>>> im.size
(330, 44)
Run Code Online (Sandbox Code Playgroud)
并且数组包含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)
Run Code Online (Sandbox Code Playgroud)
修改完阵列后,可以将其重新转换为PIL图像,如下所示:
>>> Image.fromarray(imarray)
<Image.Image image mode=L size=330x44 at 0x2786518>
Run Code Online (Sandbox Code Playgroud)
Mic*_*nan 47
我使用matplotlib来读取TIFF文件:
import matplotlib.pyplot as plt
I = plt.imread(tiff_file)
Run Code Online (Sandbox Code Playgroud)
并且I
将型的ndarray
.
根据文档虽然实际上PIL在处理TIFF时在幕后工作,因为matplotlib本身只读取PNG,但这对我来说一直很好.
还有一个plt.imsave
保存功能.
Jzl*_*325 15
您也可以使用GDAL来执行此操作.我意识到它是一个地理空间工具包,但没有什么要求你有一个制图产品.
链接到Windows的预编译GDAL二进制文件(假设这里有窗口) http://www.gisinternals.com/sdk/
要访问该阵列:
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()
Run Code Online (Sandbox Code Playgroud)
小智 11
有一个很好的软件包,tifffile
它使处理 .tif 或 .tiff 文件变得非常容易。
使用 pip 安装包
pip install tifffile
Run Code Online (Sandbox Code Playgroud)
现在,读取 numpy 数组格式的 .tif/.tiff 文件:
from tifffile import tifffile
image = tifffile.imread('path/to/your/image')
# type(image) = numpy.ndarray
Run Code Online (Sandbox Code Playgroud)
如果要将 numpy 数组保存为 .tif/.tiff 文件:
tifffile.imwrite('my_image.tif', my_numpy_data, photometric='rgb')
Run Code Online (Sandbox Code Playgroud)
或者
tifffile.imsave('my_image.tif', my_numpy_data)
Run Code Online (Sandbox Code Playgroud)
您可以在此处阅读有关此软件包的更多信息。
在图像堆栈的情况下,我发现它更易于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(mol,cmap='gray')
plt.axis('off')
# save the image
plt.savefig('output.tif', transparent=True, dpi=300, bbox_inches="tight", pad_inches=0.0)
Run Code Online (Sandbox Code Playgroud)
pylibtiff对我来说比PIL更好,后者不支持每种颜色超过8位的彩色图像。
from libtiff import TIFF
tif = TIFF.open('filename.tif') # open tiff file in read mode
# read an image in the currect 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)
Run Code Online (Sandbox Code Playgroud)
您可以使用安装pylibtiff
pip3 install numpy libtiff
Run Code Online (Sandbox Code Playgroud)
pylibtiff的自述文件也提到了tifffile.py,但是我还没有尝试过。
小智 5
您也可以使用我是作者的pytiff。
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]
Run Code Online (Sandbox Code Playgroud)
这是一个很小的模块,可能没有其他模块那么多的功能,但是它支持切片tiff和bigtiff,因此您可以读取大图像的一部分。