【记录】使用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)
相关推荐
Jasonakeke2 小时前
我的编程来时路
java·c++·python
Yvonne爱编码2 小时前
Java 中的 hashCode () 与 equals () 核心原理、契约规范、重写实践与面试全解
java·开发语言·数据结构·python·hash
HappyAcmen2 小时前
理解Python中的global与nonlocal
python
测试老哥3 小时前
Web自动化测试:Cypress 测试框架概述
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
曲幽3 小时前
FastAPI项目半夜报警吵醒你?聊聊告警这事儿怎么搞!
python·logging·fastapi·web·monitoring·webserver·health·uptimerobot
Bert.Cai3 小时前
Python模块简介
开发语言·python
2501_924952693 小时前
自动化机器学习(AutoML)库TPOT使用指南
jvm·数据库·python
Thomas.Sir3 小时前
第二章:Python3 之 列表与元组
python·列表·元组
忘忧记3 小时前
Fixture详解
开发语言·python
echome8883 小时前
Python 装饰器实战:用@syntax 优雅地增强函数功能
开发语言·python