【记录】使用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)
相关推荐
2601_962077986 分钟前
机器学习及其Python实践
pytorch·python·机器学习·tensorflow·scikit-learn
天远Date Lab9 分钟前
计算机视觉管道构建:基于天远身份证OCR实现机器学习身份信息提取
人工智能·机器学习·计算机视觉·ocr
小柯南敲键盘13 分钟前
跨马翻译批量图片视频字幕翻译,跨境电商AI智能抠图一站式工具
人工智能·python·音视频·xcode
2601_9622955820 分钟前
Python中正则表达式的知识汇总分享
python·正则表达式·模块·语法·匹配
ellenwan202624 分钟前
量化实现的难点,常在规则和流程
人工智能·python
2601_9622986725 分钟前
正则表达式引擎 哪些
java·python·perl·pcre·正则表达式引擎
gf13211111 小时前
【python_回复邮件】
android·java·python
天空属于哈夫克31 小时前
企业微信API:企业微信机器人如何开发?
python·机器人·企业微信
Patrick在香港1 小时前
把 Claude 塞进 pandas 管道:7 条脏地址实测,5 条自动清洗、2 条被闸门拦下
python·pandas·etl·claude·数据清洗
二进制漫游记1 小时前
PostgreSQL超详细入门教程:Windows安装+Python异步连接+完整增删改查实战
python·postgresql