【记录】使用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)
相关推荐
前端摸鱼匠1 分钟前
YOLOv8 环境配置全攻略:Python、PyTorch 与 CUDA 的和谐共生
人工智能·pytorch·python·yolo·目标检测
WangYaolove13147 分钟前
基于python的在线水果销售系统(源码+文档)
python·mysql·django·毕业设计·源码
AALoveTouch14 分钟前
大麦网协议分析
javascript·python
ZH154558913133 分钟前
Flutter for OpenHarmony Python学习助手实战:自动化脚本开发的实现
python·学习·flutter
xcLeigh1 小时前
Python入门:Python3 requests模块全面学习教程
开发语言·python·学习·模块·python3·requests
xcLeigh1 小时前
Python入门:Python3 statistics模块全面学习教程
开发语言·python·学习·模块·python3·statistics
一招定胜负1 小时前
入门MediaPipe:实现实时手部关键点检测
计算机视觉
YongCheng_Liang1 小时前
从零开始学 Python:自动化 / 运维开发实战(核心库 + 3 大实战场景)
python·自动化·运维开发
鸽芷咕1 小时前
为什么越来越多开发者转向 CANN 仓库中的 Python 自动化方案?
python·microsoft·自动化·cann
秋邱1 小时前
用 Python 写出 C++ 的性能?用CANN中PyPTO 算子开发硬核上手指南
开发语言·c++·python