【记录】使用Python读取Tiff图像的几种方法

文章目录

本文总结了使用 PIL Image, cv2, gdal.Open三种python package 读取多通道Tiff格式遥感影像的方法。

PIL.Image

PIL对Tiff只支持两种格式的图像:

  1. 多通道8bit图像
  2. 单通道int16, int32, float32图像

多通道多bit的tiff图像PIL不支持读取。

python 复制代码
file = Image.open(tiff_file))
# 也可以读取之后进行格式转换:
img = np.array(Image.open(tiff_file)).astype(np.float32)

cv2

cv2的读取可以选择一下几种类型:

python 复制代码
img = cv2.imread(tiff_file, arg)
# arg = -1: 8bit原通道
# arg = 2: 原深度单通道
# arg = 3: 原深度三通道

gdal

python 复制代码
from osgeo import gdal
img = gdal.Open(tiff_file)
# 查看通道数
img.RasterCount

# 读取至数组格式
img = img.ReadAsArray()
print(img.shape) > 会得到(channel, height, widht)
img = img.transpose(1,2,0) > 得到(height, width, channel)

print(np.array(img).dtype)

img = np.array(img, dtype = np.uint16)
相关推荐
databook9 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室10 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三11 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户25191624271114 小时前
Python之语言特点
python
刘立军15 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机18 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机19 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i21 小时前
django中的FBV 和 CBV
python·django
c8i21 小时前
python中的闭包和装饰器
python
这里有鱼汤1 天前
小白必看:QMT里的miniQMT入门教程
后端·python