【记录】使用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)
相关推荐
阿水实证通6 分钟前
DoubleML+FLAML实现双重机器学习超参数的自动调优(python实现路径)
人工智能·python·机器学习·实证分析
攻城狮之路人甲16 分钟前
用pycharm写的程序,点击.py无法运行闪退
ide·python·pycharm
syt_biancheng23 分钟前
大规模考试系统性能优化与风险评估
python·功能测试·selenium·性能优化·postman
热爱生活的五柒39 分钟前
在有真实标签 (Ground Truth) 的情况下,常用的指标有哪些?聚类指标有哪些?
python·指标
superman超哥40 分钟前
仓颉语言智能指针深度实战:突破 GC 与所有权的边界
c语言·开发语言·c++·python·仓颉
Elaine3361 小时前
【基于 Scikit-learn 本地数据集的垂直领域词云生成】
python·机器学习·nlp·scikit-learn·词云
3824278271 小时前
python:mysql数据库
数据库·python·mysql
中科院提名者1 小时前
KNN实战进阶:模型评估、Scikit-learn实现与Numpy手动编码
python·numpy·scikit-learn
2401_841495641 小时前
【LeetCode刷题】杨辉三角
数据结构·python·算法·leetcode·杨辉三角·时间复杂度·空间复杂度
小白开始进步1 小时前
OpenCV图像滤波:Python实战指南
人工智能·python·opencv