使用 numpy 在 Python 中处理 TIFF(导入、导出) [英] Working with TIFFs (import, export) in Python using numpy
问题描述
我需要一个 python 方法来打开 TIFF 图像并将其导入 numpy 数组,以便我可以分析和修改像素数据,然后再次将它们保存为 TIFF.(它们基本上是灰度的光强度图,代表每个像素的相应值)
我找不到有关 TIFF 的 PIL 方法的任何文档.我试图弄清楚,但只有错误模式"或不支持文件类型"错误.
我需要在这里使用什么?
首先,我从 这个页面叫做a_image.tif
.然后我像这样用PIL打开:
这显示了彩虹图像.要转换为 numpy 数组,很简单:
<预><代码>>>>导入 numpy>>>imarray = numpy.array(im)我们可以看到图像的大小和数组的形状匹配:
<预><代码>>>>imarray.shape(44, 330)>>>大小(330, 44)并且数组包含 uint8
值:
一旦你完成了对数组的修改,你就可以把它变成这样的 PIL 图像:
<预><代码>>>>Image.fromarray(imarray)<Image.Image image mode=L size=330x44 at 0x2786518>I need a python method to open and import TIFF images into numpy arrays so I can analyze and modify the pixel data and then save them as TIFFs again. (They are basically light intensity maps in greyscale, representing the respective values per pixel)
I couldn't find any documentation on PIL methods concerning TIFF. I tried to figure it out, but only got "bad mode" or "file type not supported" errors.
What do I need to use here?
First, I downloaded a test TIFF image from this page called a_image.tif
. Then I opened with PIL like this:
>>> from PIL import Image
>>> im = Image.open('a_image.tif')
>>> im.show()
This showed the rainbow image. To convert to a numpy array, it's as simple as:
>>> import numpy
>>> imarray = numpy.array(im)
We can see that the size of the image and the shape of the array match up:
>>> imarray.shape
(44, 330)
>>> im.size
(330, 44)
And the array contains uint8
values:
>>> 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)
Once you're done modifying the array, you can turn it back into a PIL image like this:
>>> Image.fromarray(imarray)
<Image.Image image mode=L size=330x44 at 0x2786518>
这篇关于使用 numpy 在 Python 中处理 TIFF(导入、导出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!