tiff处理

tiff文件的读写

使用libtiff库

参考 https://www.tjelvarolsson.com/blog/saving-16bit-tiff-files-using-python/

使用tifffile库
import numpy  as np
ar2=np.array([ar,ar,ar])
tifffile.imwrite('temp.tif', ar2, metadata={'axes': 'CYX'})
ar2 =tifffile.imread('temp.tif')
ar2.shape

 

使用raster.io库:

import numpy as np
import rasterio
from affine import Affine
from rasterio.plot import reshape_as_raster, reshape_as_image
path='/mnt/lustre/menglingxuan/buildingwolf/20200329/haerbin_18/google/anno_20200924/OffsetField/Npy/L18_93448_223176.npy'
arr=np.load(path)  #arr的shape为[2048,2048,3]

r_arr=reshape_as_raster(arr)#r_arr的shape为[3,2048,2048]


#写入tif文件,参考https://www.osgeo.cn/rasterio/topics/writing.html
with rasterio.Env():
    profile={'driver': 'GTiff', 'dtype': 'float32', 'nodata': None, 'width': W,'height': H, 'count': C, 'crs': None, 'transform': Affine(1.0, 0.0, 0.0,0.0, 1.0, 0.0), 'tiled':False, 'interleave': 'pixel'}
    profile.update(
        dtype=rasterio.float32, #数据格式
        count=3,#波段数是3
        compress='lzw')#lzw是无损压缩,可以完全恢复

    with rasterio.open('test2.tif','w',**profile) as dst_dataset:
        dst_dataset.write(r_arr) #将r_arr写入文件中

#读取tif文件

#读取tif文件,参考https://www.osgeo.cn/rasterio/topics/reading.html
import rasterio
src2=rasterio.open('test2.tif')
array2 = src2.read() #(3,2048,2048)
r_array2=reshape_as_image(array2) #(2048,2048,3)
print(np.sum(r_array2==arr),arr.shape[0]*arr.shape[1]*arr.shape[2])
src.close()        

 

 
 
 
import rasterio
from rasterio.enums import Resampling

with rasterio.open("example.tif") as dataset:
    data = dataset.read(
        out_shape=(dataset.height * 2, dataset.width * 2, dataset.count),
        resampling=resampling.bilinear
    )


 

用scipy来读取和保存tiff:

scipy读取tiff没啥问题,保存tiff时会将格式转为uint8保存,导致信息丢失

>>> import scipy.misc
>>> scipy.misc.imsave('scipy.tiff', ar)
>>> ar2 = scipy.misc.imread('scipy.tiff')
>>> ar2.dtype
dtype('uint8')
>>> np.max(ar2)
0
 
 
在wndows下使用tiffile 来存储和读取图片:
 
安装:现在该网址下下载libtiff   ( https://www.lfd.uci.edu/~gohlke/pythonlibs/),然后安装
 
使用:
 
import tifffile as tiff
im_cada = tiff.imread(FILE_cadastral2015)
tiff.imsave('./result/result_temp.tiff', result_temp)

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值