总结Python读取TIF影像的几种方法

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


总结Python读取TIF影像的几种方法


导入模块

import numpy as np
import tifffile as tf           #tifffile是tiff文件的读取库
from PIL import Image
import cv2 as  cv
import gdal

TIF文件路径

path = r'C:/Users/HP/Desktop/tif/jpeg2000/Test_Images/tif/boat4_2100.tif'

方法1:tiffile

img_tf = tf.imread(path)
print(img_tf.shape)                 #(2960, 1976, 3)

方法2:PIL

img = Image.open(path)         #可以读取单通道影像,读取3通道16位tif影像时报错(PIL.UnidentifiedImageError: cannot identify image file),支持4通道8位影像
arr = np.array(img)
print(arr.shape)

方法3:opencv

#arr = cv.imread(path,cv.IMREAD_UNCHANGED)        #(2960, 1976)
arr = cv.imread(path,1)                          #(2960, 1976, 3)   备注:4波段的影像在opencv的读取方式中,显示为前三个波段,而且读取顺序为BGR
print(arr.shape)

方法4:gdal方法1

dataset = gdal.Open(path)
arr = dataset.ReadAsArray()          #(3, 2960, 1976)
arr = arr.transpose(1, 2, 0)         #(2960, 1976, 3)
print(arr.shape)

方法5:gdal方法2

dataset = gdal.Open(path)
bands = dataset.RasterCount
for band in range(1, bands + 1):
    # 读取波段
    src_band = dataset.GetRasterBand(band)
    # 波段转数组
    band_arr = src_band.ReadAsArray()
    if band == 1:
        height = band_arr.shape[0]
        width = band_arr.shape[1]
        arr = np.zeros((height, width, bands), dtype=np.uint8)
    arr[:, :, band - 1] = band_arr
print(arr.shape)                     #(2960, 1976, 3)

在这里插入图片描述


  • 18
    点赞
  • 89
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大白曰梦想家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值