【记录】使用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)
相关推荐
WJX_KOI11 小时前
Open Notebook 一个开源的结合AI的记笔记软件
python
0思必得012 小时前
[Web自动化] 反爬虫
前端·爬虫·python·selenium·自动化
2301_8223827613 小时前
Python上下文管理器(with语句)的原理与实践
jvm·数据库·python
喵手13 小时前
Python爬虫实战:从零搭建字体库爬虫 - requests+lxml 实战采集字体网字体信息数据(附 CSV 导出)!
爬虫·python·爬虫实战·零基础python爬虫教学·csv导出·采集字体库数据·字体库字体信息采集
2301_7903009613 小时前
Python深度学习入门:TensorFlow 2.0/Keras实战
jvm·数据库·python
管牛牛14 小时前
图像的卷积操作
人工智能·深度学习·计算机视觉
程序员敲代码吗15 小时前
用Python生成艺术:分形与算法绘图
jvm·数据库·python
Yyyyy123jsjs15 小时前
如何通过免费的外汇API轻松获取实时汇率数据
开发语言·python
喵手15 小时前
Python爬虫实战:GovDataMiner —— 开放数据门户数据集元数据采集器(附 CSV 导出)!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·open data·开放数据门户数据集列表