使用numpy在Python中使用TIFF(导入,导出) [英] Working with TIFFs (import, export) in Python using numpy

查看:684
本文介绍了使用numpy在Python中使用TIFF(导入,导出)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个python方法来打开TIFF图像并将其导入到numpy数组中,以便我可以分析和修改像素数据,然后再次将它们另存为TIFF. (它们基本上是灰度的光强度图,代表每个像素的相应值)

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)

我找不到有关TIFF的PIL方法的任何文档.我试图弄清楚,但只出现错误模式"或不支持文件类型"错误.

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()

这显示了彩虹图像.要转换为numpy数组,方法很简单:

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)

该数组包含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图像,如下所示:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆