python 三维数组 存为tiff_使用numpy在Python中使用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)

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值