【记录】使用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)
相关推荐
AIGCmitutu2 小时前
Ps怎么把图片2D转3D?新手图文详细教程!
计算机视觉·photoshop·ps·美工
共享家95272 小时前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
Hgfdsaqwr3 小时前
Python在2024年的主要趋势与发展方向
jvm·数据库·python
一晌小贪欢3 小时前
Python 测试利器:使用 pytest 高效编写和管理单元测试
python·单元测试·pytest·python3·python测试
小文数模3 小时前
2026年美赛数学建模C题完整参考论文(含模型和代码)
python·数学建模·matlab
Halo_tjn3 小时前
基于封装的专项 知识点
java·前端·python·算法
Hgfdsaqwr4 小时前
掌握Python魔法方法(Magic Methods)
jvm·数据库·python
weixin_395448914 小时前
export_onnx.py_0130
pytorch·python·深度学习
s1hiyu4 小时前
使用Scrapy框架构建分布式爬虫
jvm·数据库·python
2301_763472464 小时前
使用Seaborn绘制统计图形:更美更简单
jvm·数据库·python